简体   繁体   English

从int转换为string时,我的十六进制数据被更改

[英]My hex data gets changed when converting from int to string

I'm converting a large program from python 2 to python 3. My issue is with converting an int to a string and keeping the same hex data. 我正在将大型程序从python 2转换为python3。我的问题是将int转换为字符串并保留相同的十六进制数据。 The original python 2 code seemed to just use str(), which doesn't give me the right data in 3. For reference this hexdump() function i'm using is from scapy and I've it used on strings and it has always worked in python3. 原始的python 2代码似乎只使用str(),在3中没有给我正确的数据。我使用的hexdump()函数来自scapy,用于字符串,它具有始终在python3中工作。

int_val = 0x1122 #4386
print(hex(int_val))
string = chr(int_val)
hexdump(string)

#output
#0x1122
#31 37 32 38 36

I used chr on a one byte int (17) earlier on my code and it worked this way. 我在代码前面的一个字节int(17)上使用了chr,它以这种方式工作。

#output 
#0x11
#11

I'm not sure how to convert to an int to a string while keeping the same hex data. 我不确定如何在保持相同的十六进制数据的情况下将int转换为字符串。 I've tried using str(), hex(), and chr(). 我试过使用str(),hex()和chr()。

An example based on the patient discussion in the questions comments. 在问题注释中基于患者讨论的示例。

  • Using hex()[2:] for the the conversion from 'int to string' (incl. removing the '0x'). 使用hex()[2:]从'int到string'的转换(包括删除'0x')。
  • And codecs to cross check the string with probably something comparable to hexdump (as I don't have that installed). 编解码器以可能类似于hexdump的方式交叉检查字符串(因为我没有安装)。

I tried to include the wording from the discussion in the hope that it'll help to align with the question. 我试图将讨论中的措词包括在内,以希望它有助于与问题保持一致。


(any) online hex converter as reference: (任何)在线十六进制转换器作为参考:


Code: 码:

import codecs

# pre-check: prepare example int from stringWithHexData
print('### pre-check: prepare example int from stringWithHexData ###')
stringWithHexData = '333120333720333220333820333620612063643637'
print('pre-check stringWithHexData:\n' + stringWithHexData)

str2int = int(stringWithHexData, 16)
print('pre-check int_val:\n' + str(str2int) + '\n')


# convert int 2 string
print('### convert int 2 string ###')
int_val = int(74817042136977683765116609684420893357658140325431)
print('int_val:\n' + str(int_val))
print('int_val type: ' + str(type(int_val)) + '\n')

stringWithHexData = hex(int_val)[2:]
print('stringWithHexData:\n' + stringWithHexData)
print('stringWithHexData type: ' + str(type(stringWithHexData)) + '\n')

do_something_like_hexdump = codecs.decode(stringWithHexData, "hex").decode('utf-8')
print('do_something_like_hexdump:\n' + do_something_like_hexdump)
print('do_something_like_hexdump type: ' + str(type(do_something_like_hexdump)))

Result: 结果:

### pre-check: prepare example int from stringWithHexData ###
pre-check stringWithHexData:
333120333720333220333820333620612063643637
pre-check int_val:
74817042136977683765116609684420893357658140325431

### convert int 2 string ###
int_val:
74817042136977683765116609684420893357658140325431
int_val type: <class 'int'>

stringWithHexData:
333120333720333220333820333620612063643637
stringWithHexData type: <class 'str'>

do_something_like_hexdump:
31 37 32 38 36 a cd67
do_something_like_hexdump type: <class 'str'>

Post check code: 邮寄支票代码:

# post check: and back to stringWithHexData to int
print("\n" + '### post-check: and back to stringWithHexData to int ###')
b_stringWithHexData_pos = codecs.encode(str.encode(do_something_like_hexdump), "hex_codec")
stringWithHexData_post = b_stringWithHexData_pos.decode('utf-8')
print("post check stringWithHexData2: \n" + stringWithHexData_post)
str2int_post = int(stringWithHexData_post, 16)
print('post-check int_val:\n' + str(str2int_post) + '\n')

Post check result: 发布检查结果:

### post-check: and back to stringWithHexData to int ###
post check stringWithHexData2:
333120333720333220333820333620612063643637
post-check int_val:
74817042136977683765116609684420893357658140325431

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM