473,473 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Converting Hex to decimal

Hello group:

I have a file containing hex data. I would like to convert the
contents of the file to decimal by reading each by sequentially (one
byte at the time). Is there a module that has such functionality or
does someone have the relevant code?

Also, is there an oracle module available for windows xp and hp-ux?

Much appreciated

dean

please reply to diabolik(nospam)@uku.co.uk remove (nospam)
Jul 18 '05 #1
7 5407
Hello Dean,
I have a file containing hex data. I would like to convert the
contents of the file to decimal by reading each by sequentially (one
byte at the time). Is there a module that has such functionality or
does someone have the relevant code?

Have a look at `array' or `struct' modules.
If you just want to read "raw" bytes you can do:
for c in open("some_file").read():
print hex(ord(c))
(Note that this read *all* the file into memory)

HTH.
--
-------------------------------------------------------------------------
Miki Tebeka <mi*********@zoran.com>
http://tebeka.web1000.com
The only difference between children and adults is the price of the toys.

Jul 18 '05 #2
dean wrote:
I have a file containing hex data.
What does the term "hex data" mean to you? Does it mean
binary? Does it mean that there are only characters in
the range 0-9A-F and that each pair of characters represents
a single byte? Something else? It is not a term with a
precisely defined meaning.
I would like to convert the
contents of the file to decimal by reading each by sequentially (one
byte at the time). Is there a module that has such functionality or
does someone have the relevant code?


Definitely Python can do it, possibly with the struct module,
but it all depends on what "hex data" means. Do you have
an example of what input will translate to what output?

-Peter
Jul 18 '05 #3
On 1 Jun 2004 01:51:23 -0700, de***@redstone.co.uk (dean) wrote:
Hello group:

I have a file containing hex data. I would like to convert the
contents of the file to decimal by reading each by sequentially (one
byte at the time). Is there a module that has such functionality or
does someone have the relevant code?

Also, is there an oracle module available for windows xp and hp-ux?

Much appreciated

dean

please reply to diabolik(nospam)@uku.co.uk remove (nospam)

The hex data in file is a bit confusing. I can think of two ways you
could mean this.

For the one that is a file with text that reads like:

2344
fabd
3343
43ae
fe34

Hmm, how's about int()? It accepts a radix.
int('aefdcba',16) 183491770 int('11',16)

17

The other way would be with struct/array like others have mentioned.
<{{{*>


Jul 18 '05 #4
Hello Group

Thanks for your responses so far. I have not been clear it seems so
here it goes:

The file is binary. Using the standard hpux or linux hexdump (xxd or
xd) this is the output that is obtained:

0000000: 5c1a 0100 0100 ff35 0399 7272 9903 0002 \......5..rr....
0000010: 010e 0300 050a 0214 ffff ffff 5cd4 2e01 ............\...
0000020: fbb9 5900 b400 0000 0020 2000 b502 5423 ..Y...... ...T#
0000030: 3a71 2e11 00ff ffff ffff ffff ffff ffff :q..............
0000040: ffff ffff ff00 ff07 0209 3519 4fff ffff ..........5.O...
0000050: ffff ffff ffff ffff ffff ffff ffff ffff ................
0000060: ffff ffff ffff ffff ffff ffff ffff ffff ................
0000070: ffff ffff ffff ffff ffff ffff ffff ffff ................
0000080: ffff ffff ffff ff00 5435 3617 040a d207 ........T56.....
0000090: 3e05 3717 040a d207 0000 0000 2c01 6000 >.7.........,.`.
00000a0: 0141 01a8 0005 030b 0300 00ff ffff ffff .A..............
00000b0: ffff ffff ffff ffff ffff ff00 0000 0000 ................
00000c0: 0300 0000 5435 3617 040a d207 0b00 0000 ....T56.........
00000d0: 0000 0000 fcb9 5900 b400 0000 0020 2000 ......Y...... .
00000e0: b602 5423 3b71 2e11 00ff ffff ffff ffff ..T#;q..........
00000f0: ffff ffff ffff ffff ff00 2094 3965 0702 .......... .9e..
0000100: 0935 194f ffff ffff ffff ff07 0209 3519 .5.O..........5.
0000110: 4fff ffff ffff ffff ffff ffff ffff ffff O...............
0000120: ffff ffff ffff ffff ffff ffff ffff ffff ................
0000130: ffff ffff ffff ffff ffff ff00 1536 3617 .............66.

Each hex character is 8 bits. I would like to convert one byte at the
time and pipe the converted data into a new file.

Thanx in advance
Regards
Dean
Jul 18 '05 #5


dean wrote:
The file is binary. Using the standard hpux or linux hexdump (xxd or
xd) this is the output that is obtained:

0000000: 5c1a 0100 0100 ff35 0399 7272 9903 0002 \......5..rr....
0000010: 010e 0300 050a 0214 ffff ffff 5cd4 2e01 ............\...
0000020: fbb9 5900 b400 0000 0020 2000 b502 5423 ..Y...... ...T#
0000030: 3a71 2e11 00ff ffff ffff ffff ffff ffff :q..............
0000040: ffff ffff ff00 ff07 0209 3519 4fff ffff ..........5.O...
0000050: ffff ffff ffff ffff ffff ffff ffff ffff ................
0000060: ffff ffff ffff ffff ffff ffff ffff ffff ................
0000070: ffff ffff ffff ffff ffff ffff ffff ffff ................
0000080: ffff ffff ffff ff00 5435 3617 040a d207 ........T56.....
0000090: 3e05 3717 040a d207 0000 0000 2c01 6000 >.7.........,.`.
00000a0: 0141 01a8 0005 030b 0300 00ff ffff ffff .A..............
00000b0: ffff ffff ffff ffff ffff ff00 0000 0000 ................
00000c0: 0300 0000 5435 3617 040a d207 0b00 0000 ....T56.........
00000d0: 0000 0000 fcb9 5900 b400 0000 0020 2000 ......Y...... .
00000e0: b602 5423 3b71 2e11 00ff ffff ffff ffff ..T#;q..........
00000f0: ffff ffff ffff ffff ff00 2094 3965 0702 .......... .9e..
0000100: 0935 194f ffff ffff ffff ff07 0209 3519 .5.O..........5.
0000110: 4fff ffff ffff ffff ffff ffff ffff ffff O...............
0000120: ffff ffff ffff ffff ffff ffff ffff ffff ................
0000130: ffff ffff ffff ffff ffff ff00 1536 3617 .............66.

Each hex character is 8 bits. I would like to convert one byte at the
time and pipe the converted data into a new file.


Ok, you've got the hexdump and, as far as I understand,
you want the binary data back, byte by byte.

import binascii

def makeBinary (f):
for line in f:
words = line.split () [1:9]
for word in words:
yield binascii.unhexlify (word [:2])
yield binascii.unhexlify (word [2:])

Then use the generator this way:

for byte in makeBinary (open ("hexdumpfile", "r")):
[...]

I did not test it, but at least it is an outline of
the way it can be done.

Greetings,

Holger

Jul 18 '05 #6
Hello Dean,
Each hex character is 8 bits. I would like to convert one byte at the
time and pipe the converted data into a new file.

This is not tested but should be in the lines of what you're trying to
do:

from array import array
a = array("B")
a.fromstring(open("input_file", "rb").read()) # Read all file
out = open("output_file", "wb")
for bit in a:
new_bit = convert(bit)
out.write(new_bit)
HTH.
--
-------------------------------------------------------------------------
Miki Tebeka <mi*********@zoran.com>
http://tebeka.web1000.com
The only difference between children and adults is the price of the toys.

Jul 18 '05 #7
dean wrote:
Thanks for your responses so far. I have not been clear it seems so
here it goes:
Judging by the subsequent responses, you're still not being
clear. :-)
The file is binary.
In other words, it is *not* "hex". It's binary. Plain and simple.
Is that true?
Using the standard hpux or linux hexdump (xxd or
xd) this is the output that is obtained:
So the only hex thing about it is that you can display the contents
using a program that displays binary content as hex?
0000000: 5c1a 0100 0100 ff35 0399 7272 9903 0002 \......5..rr....
0000010: 010e 0300 050a 0214 ffff ffff 5cd4 2e01 ............\...
But the actual file that you have is the original, binary
file, right? Not just this hexdump output?
Each hex character is 8 bits.
Do you mean that each byte is 8 bits?
I would like to convert one byte at the
time and pipe the converted data into a new file.


Convert in what way? Or do you really mean that you have only
the hexdump output and want to convert it back to the raw binary
file from which it was originally generated, as the last several
respondents have assumed?

Otherwise, it really sounds like you just want to read the file
byte-by-byte and "convert" the bytes in some way, writing them
to a new file. There is nothing special about doing this: you
would just use open() and .read() and .write() like with any
other file.

-Peter
Jul 18 '05 #8

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

Similar topics

0
by: Marcelo López | last post by:
Hello everybody. I'm working with an office pivot table and i have to convert from its currency type to a decimal (in C#) , operate with the result and then re-convert it to the currency type to...
6
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
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...
0
by: Marcelo López | last post by:
Hello everybody. I'm working with an office pivot table and i have to convert from its currency type to a decimal (in C#) , operate with the result and then re-convert it to the currency type to...
7
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
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...
116
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
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
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
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
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
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
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
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
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
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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...

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.