将用户输入转换为十六进制,加入 NULL?

Converting user input into hex, joined with NULL?

我需要将用户输入转换为十六进制,但每个字符之间有 NULL 字符。

例如:

用户输入 = 111

输出=b'3100310031

我最好的尝试看起来像这样,但它离...

import binascii
content5 = rb"111"
print(content5)
joiner = "00"
content4 = binascii.hexlify(content5)
print(content4)
content3 = joiner.join(str(content4))
print(content3)
content2 = bytes(content3, 'utf-8')
print(content2)
content = binascii.hexlify(content2)
print(content)

但是这个returns:

b'111'
b'313131'
b00'00300100300100300100'
b"b00'00300100300100300100'"
b'62303027303033303031303033303031303033303031303027'
b'3632333033303237333033303333333033303331333033303333333033303331333033303333333033303331333033303237'

如有任何帮助,我们将不胜感激!

Hexlify 将输入字节字符串的每个字节更改为其(2 字节)十六进制表示形式。一次只处理一个字节并加入结果:

import binascii
content5 = rb"111"
print(content5)
joiner = "00"
content4 = joiner.encode().join(binascii.hexlify(bytes((i,))) for i in content5)
print(content4)

它给出了预期的结果:

b'111'
b'3100310031'