Trouble converting hex to decimal?
Adam DePrince
adam at cognitcorp.com
Sun Feb 6 02:33:49 EST 2005
On Sat, 2005-02-05 at 12:02, Steve Holden wrote:
> Pedro Werneck wrote:
> > The problem is that '\x00' is a escape sequence...
> > Try something like this:
> >>>>x = '\x00'
> >>>>int(repr(x)[3:-1], 16)
> > 0
> >>>>x = '\x15'
> >>>>int(repr(x)[3:-1], 16)
> > 21
> > On Sat, 05 Feb 2005 06:51:32 -0700
> > Earl Eiland <eee at nmt.edu> wrote:
> >>I'm trying to process the IP packet length field, as recorded by pcap
> >>(Ethereal) and recovered using pcapy. When I slice out those bytes, I
> >>get a value that shows in '\x00' format, rather than '0x00'. Neither
> >>int() nor eval() are working. How do I handle this?
> >>Earl Eiland
> Talk about making things difficult!
> >>> x = '\x00'
> >>> ord(x)
> 0
> >>> x = '\x15'
> >>> ord(x)
> 21
> >>>
Ethereal's emission of \x00 shouldn't make your life any more
difficult. The conversion of \0xx takes place in the eval. If you
store the string \x00 in a file and call open.read or command.getoutput,
you will get the python string "\\x00". Stuff you read from a file
arrives un-de-escaped.
The solution is simple.
mystring.replace( "\x","0x" )
Adam DePrince
More information about the Python-list
mailing list