0

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

  • there's actually a way to convert your whole python2 project to python3, did you try it ? link: https://docs.python.org/2/library/2to3.html – Inspi Jul 31 '19 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. – CMMCD Jul 31 '19 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 – Matthew Jul 31 '19 at 19:04
  • @CMMCD This function I'm testing out is supposed to take an int, convert to a string with the same hex data, add a length to the hex data, and return a string of the length + hex data. I'm having issues with the conversion part, everything else is working. My code I included is just how the int to str is working. – Matthew Jul 31 '19 at 19:16
  • Converting a int to string won't give the same hex value. If you want the hex values of the integer as a string that is what the hex value gives you. Otherwise, could you provide a few examples of sample input and desired output? – CMMCD Jul 31 '19 at 19:21
  • 1
    `chr(int_val)` is treating `int_val` as a Unicode code point, and returning that character. – Barmar Jul 31 '19 at 19:23
  • 1
    Then `hexdump()` is showing the UTF-8 bytes of the character. – Barmar Jul 31 '19 at 19:23
  • @CMMCD To test the function I start by making an int 0x1122. This should be 1122 in hex. In python 2 which works the str function returns an unreadable string but the hex data is 11 22 – Matthew Jul 31 '19 at 19:33
  • @Barmar Do you have any idea on how I can do what I'm trying to do then. I just want the type of the value to be a str and the hex value to be the same – Matthew Jul 31 '19 at 20:49
  • Edit the question to show your desired result. – Barmar Jul 31 '19 at 20:57
  • `hex()` returns a string, why can't you just use that? – Barmar Jul 31 '19 at 20:57
  • @Barmar hex returns the hex data as a string rather than a string with the hex data. If there was a way for me to make a variable with this as hex info it would work fine. It doesn't seem like there's a way to do what i want so I will probably just use hex and change other functions called after – Matthew Jul 31 '19 at 21:28
  • What's the difference between "hex data as a string" and "string with the hex data"? – Barmar Jul 31 '19 at 21:32
  • If you edit the question to show your desired result, someone can surely help you get it. – Barmar Jul 31 '19 at 21:32
  • @Barmar When i say hex data as a string i mean the string is literally something like '1122' and when i say str with hex data the string is just some random characters/numbers that but when you do something like hexdump it prints '1122' because that represents the string. – Matthew Jul 31 '19 at 21:41
  • Maybe you're looking for `struct.pack()` – Barmar Jul 31 '19 at 21:46

1 Answers1

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
MagnusO_O
  • 121
  • 1
  • 6