niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » python
Trouble converting hex to decimal?
Post new topic   Reply to topic Page 1 of 1 [8 Posts] View previous topic :: View next topic
Author Message
Adam DePrince
*nix forums beginner


Joined: 06 Feb 2005
Posts: 13

PostPosted: Sun Feb 06, 2005 6:33 am    Post subject: Re: Trouble converting hex to decimal? Reply with quote

On Sat, 2005-02-05 at 12:02, Steve Holden wrote:
Quote:
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@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
Back to top
Miki Tebeka
*nix forums addict


Joined: 22 Feb 2005
Posts: 61

PostPosted: Sun Feb 06, 2005 6:23 am    Post subject: Re: Trouble converting hex to decimal? Reply with quote

Hello Earl,

Quote:
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?
Try using "array" or "struct" modules.


HTH.
--
------------------------------------------------------------------------
Miki Tebeka <miki.tebeka@zoran.com>
http://tebeka.bizhat.com
The only difference between children and adults is the price of the toys
Back to top
Jorgen Grahn
*nix forums Guru Wannabe


Joined: 25 Feb 2005
Posts: 107

PostPosted: Sat Feb 05, 2005 4:53 pm    Post subject: Re: Trouble converting hex to decimal? Reply with quote

On Sat, 05 Feb 2005 06:51:32 -0700, Earl Eiland <eee@nmt.edu> wrote:
Quote:
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?

From my unpublished protocol analyzing hack:

class Ip:
"IPv4 header"
def __init__(self, frame):
(vi, tos, tlen,
id, ffoff,
ttl, proto, checksum,
source,
dest) = struct.unpack('! BBH HH BBH LL', frame[:20])
self.len = 4 * (vi & 0xf)
if proto==6:
self.proto=Tcp
elif proto==17:
self.proto=Udp
elif proto==1:
self.proto=Icmp
self.source = Address(source)
self.dest = Address(dest)

That doesn't take IP options into account (or are they part of Ip.len? I
forget.), but it has the nifty feature that IP.proto tells the caller what
factory function (if any) she should feed the rest of the frame into.

/Jorgen

--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
Back to top
Steve Holden
*nix forums Guru


Joined: 22 Feb 2005
Posts: 1255

PostPosted: Sat Feb 05, 2005 4:02 pm    Post subject: Re: Trouble converting hex to decimal? Reply with quote

Pedro Werneck wrote:

Quote:
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




On Sat, 05 Feb 2005 06:51:32 -0700
Earl Eiland <eee@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!


Quote:
x = '\x00'
ord(x)
0
x = '\x15'
ord(x)
21


regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Back to top
Pedro Werneck
*nix forums beginner


Joined: 27 Jul 2005
Posts: 12

PostPosted: Sat Feb 05, 2005 3:52 pm    Post subject: Re: Trouble converting hex to decimal? Reply with quote

Hi

The problem is that '\x00' is a escape sequence...

Try something like this:


Quote:
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@nmt.edu> wrote:

Quote:
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

--
http://mail.python.org/mailman/listinfo/python-list
Back to top
Alan McIntyre
*nix forums beginner


Joined: 03 Mar 2005
Posts: 18

PostPosted: Sat Feb 05, 2005 1:12 pm    Post subject: Re: Trouble converting hex to decimal? Reply with quote

Earl,

Try this:

Quote:
ord('\x00')
0


or:

Quote:
import struct
struct.unpack('b', '\x00')
(0,)


If you're needing to pull values out of multiple bytes (shorts, longs,
floats, etc.), have a look at the struct module. Here's an example:

Quote:
struct.unpack('f', '\x00\x00(B')
(42.0,)


Hope this helps,
Alan

Earl Eiland wrote:
Quote:
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
Back to top
Steve Holden
*nix forums Guru


Joined: 22 Feb 2005
Posts: 1255

PostPosted: Sat Feb 05, 2005 1:03 pm    Post subject: Re: Trouble converting hex to decimal? Reply with quote

Earl Eiland wrote:

Quote:
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


You could either reconstruct the value yourself from the ord() values of
the individual bytes, or look at the struct module which can help you
with this kind of decoding task.

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Back to top
Earl Eiland
*nix forums beginner


Joined: 02 Mar 2005
Posts: 35

PostPosted: Sat Feb 05, 2005 12:51 pm    Post subject: Trouble converting hex to decimal? Reply with quote

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
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [8 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 11:32 pm | All times are GMT
navigation Forum index » Programming » python
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Trouble enabling auth on postfix and sasl dklugmann Postfix 0 Fri Feb 29, 2008 11:02 pm
No new posts Trouble Declaring 3D Array in Header File free2klim C++ 1 Fri Jul 21, 2006 4:07 am
No new posts to_char number format with optional decimal-point? Martin T. Oracle 3 Thu Jul 20, 2006 10:53 am
No new posts converting array values to monomaniac21 PHP 11 Thu Jul 20, 2006 10:17 am
No new posts Converting a web to pdf or ..‍ Bayazee python 2 Wed Jul 19, 2006 12:47 pm

Car Credit | Free Games | Credit Cards | Debt Consolidation | Free Advertising
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.6528s ][ Queries: 20 (0.5050s) ][ GZIP on - Debug on ]