I am using Python 3.
I am also using the ord() function in my script.
If I type ord('\xff')
in the console it returns 255
Is there a way to insert \xff
(which is stored in a variable)
into the ord()
function without getting
error:
TypeError: ord() expected a character, but string of length 4 found
How does ord('\xff')
work? ord() doesn't read strings? Whats it reading then?
Are you receiving this data from some user input? In this case, what you meant to read as a single character was read literally as a string of size 4.
One workaround is to use
ast.literal_eval
: