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