Re: Trouble converting hex to decimal?

P

Pedro Werneck

Hi

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



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
 
S

Steve Holden

Pedro said:
Hi

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





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!

regards
Steve
 
A

Adam DePrince

Pedro said:
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

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
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top