On Fri, Dec 12, 2008 at 12:28 AM, <chengang.beij...@gmail.com> wrote:
> 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? > Is that supposed to be a single integer or 4 integers? Either way, you'd use a for loop to iterate over each character. 4 integers : >>> a = '\xf0\xf0\xff\xfe' >>> b = [ord(ch) for ch in a] >>> b [240, 240, 255, 254] 1 really large integer: >>> a = '\xf0\xf0\xff\xfe' >>> i = 0 >>> for ch in a : ... i = i << 8 ... i += ord(ch) ... >>> i 4042326014L > > > Br, Chen Gang > > On Dec 12, 12:40 pm, Steve Holden <st...@holdenweb.com> wrote: > > chengang.beij...@gmail.com wrote: > > > '\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 > > > > > On Dec 12, 12:06 pm, Tommy Nordgren <tommy.nordg...@comhem.se> wrote: > > >> On Dec 12, 2008, at 4:48 AM, chengang.beij...@gmail.com wrote: > > > > >>> int('\xf0',16) doesn't work, any way to do that? > > >>> -- > > >>>http://mail.python.org/mailman/listinfo/python-list > > >> Should be int('10',16) > > >> or int('0x10',16) > > > > 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: > > > > > > > > >>> ord('A') > > 65 > > >>> chr(65) > > 'A' > > >>> ord('\xf0') > > 240 > > >>> chr(240) > > '\xf0' > > >>> hex(240) > > '0xf0' > > > > So just apply the ord() function to the character and you'll get its > > integer value! > > > > regards > > Steve > > -- > > Steve Holden +1 571 484 6266 +1 800 494 3119 > > Holden Web LLC http://www.holdenweb.com/ > > -- > http://mail.python.org/mailman/listinfo/python-list >
-- http://mail.python.org/mailman/listinfo/python-list