473,776 Members | 1,557 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 2120
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
3198
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 box as decimal or do I just have to make another decimal variable .. So I took a guess hoping CDec would come up blue if I type it in with no squigglies on the line. But is this actually converting it to decimal. Next if so, I do not like the...
2
7679
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 int type). I want to store the number as binary stream, and I don't know how to convert large decimal string (char*) to binary stream. Converting using MOD would last for hours with really big numbers, and in fact I don't know any other ways to...
7
4600
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 first to make sure it is numeric. Using the is keyword doesn't work (strings cannot be cast as decimal so false is always returned) and catching an exception from Convert.ToDecimal or decimal.Parse is too slow. Does anyone know of any methods...
1
9979
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 += Convert.toInt32(strVal);
1
3776
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 gateway. Verisign requires that the amount passed in is in the format ###.##, so if I have 250.50 its passed in exactly as that. What is happening is that when I use 250.50 the decimal datatype drops the last zero making this 250.5 which causes the...
3
2157
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
35994
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 problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going haywire. So my question is, given this code: int main() { float f = 59.89F;
10
1997
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 txtradius that I enter a radius into. I then have a lable that I want the surface area to be displayed in called lblsurface
4
3456
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
5126
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 amount = 126.60; double interestRate = .075; double interest = amount * interestRate;
0
9628
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10289
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8952
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7471
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5367
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
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 we have to send another system
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.