|
|
|
|
|
|
| Author |
Message |
Adam DePrince *nix forums beginner
Joined: 06 Feb 2005
Posts: 13
|
Posted: Sun Feb 06, 2005 6:33 am Post subject:
Re: Trouble converting hex to decimal?
|
|
|
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
|
Posted: Sun Feb 06, 2005 6:23 am Post subject:
Re: Trouble converting hex to decimal?
|
|
|
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
|
Posted: Sat Feb 05, 2005 4:53 pm Post subject:
Re: Trouble converting hex to decimal?
|
|
|
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
|
Posted: Sat Feb 05, 2005 4:02 pm Post subject:
Re: Trouble converting hex to decimal?
|
|
|
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
|
Posted: Sat Feb 05, 2005 3:52 pm Post subject:
Re: Trouble converting hex to decimal?
|
|
|
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
|
Posted: Sat Feb 05, 2005 1:12 pm Post subject:
Re: Trouble converting hex to decimal?
|
|
|
Earl,
Try this:
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
|
Posted: Sat Feb 05, 2005 1:03 pm Post subject:
Re: Trouble converting hex to decimal?
|
|
|
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
|
Posted: Sat Feb 05, 2005 12:51 pm Post subject:
Trouble converting hex to decimal?
|
|
|
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 |
|
 |
|
|
The time now is Thu Jan 08, 2009 11:32 pm | All times are GMT
|
|
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
|
|