我有这段代码。
character = input("Enter a character: n")
def ASCIItoHEX(ascii):
hexa = []
for i in range(len(ascii)):
ch = ascii[i]
in1 = ord(ch)
part = hex(in1)
hexa += part
return hexa
if __name__ == '__main__':
hexadecimal = ASCIItoHEX(character)
print(hexadecimal)
当我输入"0"时,它给我这样的输出;1111〃:
['0','x','3','1','0','x],'3'、'1'、'0'、'x'、'3'、'1'、'0'、'0'、'x]、'3'和'1']
但我想要这样的输出:
[00x31','0x31','0x32','0x31']
如何获得此输出?
使用list.append()
而不是+=
来附加整个字符串。否则,字符串将被视为序列并拆分为字符。
character = input("Enter a character: n")
def ASCIItoHEX(ascii):
hexa = []
for i in range(len(ascii)):
ch = ascii[i]
in1 = ord(ch)
part = hex(in1)
hexa.append(part)
return hexa
if __name__ == '__main__':
hexadecimal = ASCIItoHEX(character)
print(hexadecimal)
另一种方式是
hexa += [part]
将两个列表连接起来。