Python Forum
Question about change hex string to integer sting in the list (python 2.7) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Question about change hex string to integer sting in the list (python 2.7) (/thread-33756.html)



Question about change hex string to integer sting in the list (python 2.7) - lzfneu - May-24-2021

Hi all,

My attached short python 2.7 code in the attachment output a hex string in the list:
['0x74', '0x65', '0x73', '0x74']

I tried and have no idea how to change the output result from hex string to hex integer in the following like this for python 2.7 in the list:
[0x74, 0x65, 0x73, 0x74]

Thank you very much in advance.


RE: Question about change hex string to integer sting in the list (python 2.7) - bowlofred - May-24-2021

There is no "hex integer". There are just integers. When python displays an integer with the default string representation, it's as a decimal.

In other words, why do you want [0x74, 0x65, 0x73, 0x74] instead of [116, 101, 115, 116]? If it's to display, you can print it that way. Otherwise, it shouldn't matter.

>>> [ord(x) for x in "test"]
[116, 101, 115, 116]
>>> print("[" + ", ".join(hex(ord(x)) for x in "test") + "]")
[0x74, 0x65, 0x73, 0x74]