要将字符串转换为ASCII码,可以使用Python内置函数ord()。该函数将字符作为参数,返回它的ASCII码值。

例如,将字符串“hello”转换为ASCII码:

string = "hello"
ascii_string = ""

for char in string:
    ascii_value = ord(char)
    ascii_string += str(ascii_value) + " "

print(ascii_string)

输出:

104 101 108 108 111 

这将把字符串中的每个字符转换为它的ASCII码,并将结果存储在一个字符串中,使用空格分隔。

字符串转ASCII

原文地址: https://www.cveoy.top/t/topic/HLi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录