十六进制字符串以字符串格式正确,格式错误,一旦传递给unhexlify()

问题描述 投票:0回答:1
def craft_integration(xintegration_time):

 integration_time = xintegration_time
 integration_time_str = str(integration_time)
 integration_time_str = integration_time_str.encode('utf-8')
 integration_time_hex = integration_time_str.hex()

 return integration_time_hex

def send_set_integration(xtime):

 int_time_hex = decoder_crafter.craft_integration(xtime)

 set_hex = "c1c000000000000010001100000000000000000000000004"+int_time_hex+"1400000000000000000000000000000000000000c5c4c3c2"
 set_hex = str(set_hex)
 print(set_hex)
 set_hex = unhexlify(set_hex)

例如,输入为'1000'。使用craft_integration()变为31303030。然后将其插入默认的十六进制字符串。

输出为:

c1c000000000000010001100000000000000000000000004314310300301400000000000000000000000000000000000000c5c4c3c2

当使用unhexlify()时,输出为:

b'\ xc1 \ xc0 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x10 \ x00 \ x11 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x041000 \ x14 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ xc5 \ xc4 \ xc3 \ xc2 '

\ x041000是\ x04和1000的和,它是原始输入值,而不是转换后的值。

为什么会这样?

python hex
1个回答
0
投票
实际上,您所期望的值只是通过bytes.__repr__的默认实现将您想要的值呈现为表单,您并没有想到它对您想要的内容没有帮助。
© www.soinside.com 2019 - 2024. All rights reserved.