how to convert ‘\xf0' to 0xf0 ?

C

chengang.beijing

'\xf0' is the value read from a binary file, I need to change this
kinds strings to int for further processing...
if it is in C, then '\xf0' is an integer and it can be handled
directly, but in python, it is a string.

and both int('10',16) and int('0x10',16) returns 16.

Br, Chen Gang
 
S

Steve Holden

'\xf0' is the value read from a binary file, I need to change this
kinds strings to int for further processing...
if it is in C, then '\xf0' is an integer and it can be handled
directly, but in python, it is a string.

and both int('10',16) and int('0x10',16) returns 16.

Br, Chen Gang

It seems that you want the integer value of a character you read in from
a file. Is this correct? Note that '\xf0' is the interpreter's way of
representing a one-character string whose only character has the
hexadecimal value f0, because the actual character is not printable: the
backslash has a special meaning in character string literals.

Any one-character string, however, can be converted to the equivalent
integer value using the ord() function. You can convert the other way
using the chr() function:

So just apply the ord() function to the character and you'll get its
integer value!

regards
Steve
 
C

chengang.beijing

Hi,

ord('\xf0') works and it only works for char. Do you know any way to
convet
'\xf0\xf0' and '\xf0\xf0\xff\xfe' to integer?


Br, Chen Gang
 
S

Stephen Thorne

Hi,

ord('\xf0') works and it only works for char. Do you know any way to
convet
'\xf0\xf0' and '\xf0\xf0\xff\xfe' to integer?

Perhaps you want the 'struct' module.
(-3856, -252641282)

help(struct) for more information
--
Regards,
Stephen Thorne
Development Engineer
NetBox Blue - 1300 737 060

Scanned by the NetBox from NetBox Blue
(http://netboxblue.com/)
 
S

Steve Holden

Looks like you need the struct module. That can convert binary fields of
various lengths into the appropriate Python types, and vice versa.

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,433
Messages
2,571,683
Members
48,796
Latest member
Greg L.

Latest Threads

Top