Converting hexadecimal to decimal gives unexpected result

I am working with "Learn Python the Hard Way".

On Page 82 there is a hexadecimal encoded string.

rawBytes = b'\xe6\x96\x87\xe8\xa8\x80'

When I try to convert only one hexadecimal value to decimal value

int(0xxe6)

I get the error

SyntaxError: invalid hexadecimal literal

Why is that?

1 Answer

Hexadecimal values are composed of numbers 0-9 and/or A to F. You are using the letter 'x' twice in a row which causes the error. The escape sequence in the byte literal '\x' has the same meaning as '0x'. So try int(0xe6) and you are golden.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like