1

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. 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.

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.

#output 
#0x11
#11

I'm not sure how to convert to an int to a string while keeping the same hex data. I've tried using str(), hex(), and chr().

16
  • there's actually a way to convert your whole python2 project to python3, did you try it ? link: docs.python.org/2/library/2to3.html Commented Jul 31, 2019 at 18:56
  • When you convert from int to string you are turning an integer into multiple characters so the hex value changes. If you share your use case we might be able to point you in the right direction for what you really want to be doing. Commented Jul 31, 2019 at 18:58
  • @Inspi I used 2to3 to fix all the prints and things like .iteritems. However looking at 2to3 fixers I don't think it would change anything i'm fixing now, Which is mostly string handling and stuff like this Commented Jul 31, 2019 at 19:04
  • 1
    chr(int_val) is treating int_val as a Unicode code point, and returning that character. Commented Jul 31, 2019 at 19:23
  • 1
    Then hexdump() is showing the UTF-8 bytes of the character. Commented Jul 31, 2019 at 19:23

1 Answer 1

0

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').
  • And codecs to cross check the string with probably something comparable to hexdump (as I don't have that installed).

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.