Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print python Byte variable?

Tags:

python-3.x

I have this byte variable

testByte = b"\x02\x00\x30\x03\x35"

I would like to print it out

I've tried:

listTestByte = list(testByte)

However, I'm getting

[2, 0, 48, 3, 35]

I'm expecting it to be:

[2, 0, 30, 3, 35]

like image 785
user1872384 Avatar asked Oct 29 '25 14:10

user1872384


1 Answers

What you have are hexadecimal values. So what you're getting is what you should be getting. (Except that you should be getting [2, 0, 48, 3, 53] and not [2, 0, 48, 3, 35].)

If you want the list to have what you have in hexadecimal you can try converting it back to hexadecimal.

testByte = b"\x02\x00\x30\x03\x35"
listTestByte = list(testByte)
print(listTestByte)             # [2, 0, 48, 3, 53]
listTestByteAsHex = [int(hex(x).split('x')[-1]) for x in listTestByte]
print(listTestByteAsHex)        # [2, 0, 30, 3, 35]

Or use string operations, to split at '\x' depending on your purpose.

like image 66
sP_ Avatar answered Oct 31 '25 04:10

sP_



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!