473,508 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 18 '05 #1
3 2099
Earl Eiland 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


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/
Jul 18 '05 #2
Earl,

Try this:
ord('\x00') 0

or:
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:
struct.unpack('f', '\x00\x00(B')
(42.0,)

Hope this helps,
Alan

Earl Eiland 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

Jul 18 '05 #3
On Sat, 05 Feb 2005 06:51:32 -0700, Earl Eiland <ee*@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?


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!
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3175
by: Newbee Adam | last post by:
I have been reading in help how I need to use decimal becuase currency does not exist like I used in vb6. I had a difficult time on google and msdn finding how or if I can take the value of text...
2
7657
by: Mariusz Sakowski | last post by:
I'm writing class which will be able to store large numbers (my ambition is to make it able to operand on thousands of bits) and perform various operations on it (similiar to those available with...
7
4578
by: Paul K | last post by:
I'm writing a small component that needs to be as fast as possible. The component needs to convert a string to decimal during the course of it's processing. However, I need to test the string...
1
9960
by: mdawoodk | last post by:
i am getting error "input string was not in correct format" when converting a string decimal into integer value. code is like this: string strVal = ""; int nVal = 0; strVal = "14.9"; nVal...
1
3762
by: Aaron Prohaska | last post by:
Good morning everyone, I'm having problems when I convert the SQL Server datatype smallmoney to the .net datatype decimal. I am doing this to pass a dollor amount to the Verisign payment...
3
2145
by: Espen | last post by:
Code: <SCRIPT language="JavaScript"> Rente = "5,9"; Rente2 = parseFloat(Rente.replace(",",".")/100); document.write(Rente2 + '<br>'); Rente = "4,9";
116
35672
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
10
1955
by: Ron | last post by:
I want to calculate the surface area of a sphere from an inputed radius with option strict on. I guess I am not converting something correctly. Here is what I am doing: I have a textbox...
4
3444
by: Jeff | last post by:
Hey ..NET 2.0 In my code am I trying to convert an int value into a decimal: decimal d = 0; int k = 87664; d = Convert.ToDecimal(k/100);
2
5076
by: clintonb | last post by:
Victor said: The double value that I'm trying to convert to GCSMoney (which is implemented as cents) was produced by multiplying a dollar amount by an interest rate to get interest. double...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.