4

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]

2
  • 3
    The 30 in \x30 is a hexadecimal value. int values are printed using their decimal values. What exactly is it you want to do? What would you expect b"\xff" to print as? Commented Aug 17, 2018 at 15:52
  • 3
    Also since it's hexadeicmal value you should be getting [2, 0, 48, 3, 53] Commented Aug 17, 2018 at 15:53

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thx. I'm hitting "invalid literal for int () with base 10" for listTestByte with value [2,0,53,3,0,0,0,3,61]. Why is it so?
Hexadecimal values range from 0-9 and a-f. And 61 in decimal is 3D in hex. So that doesn't convert to int. So remove int() to get the value as string. If you still need 61 then I'd suggest using string operations.

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.