Converting Hex to decimal

D

dean

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)
 
M

Miki Tebeka

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.
 
P

Peter Hansen

dean said:
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
 
F

fishboy

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.
17

The other way would be with struct/array like others have mentioned.
 
D

dean

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
 
?

=?ISO-8859-1?Q?Holger_T=FCrk?=

dean said:
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
 
M

Miki Tebeka

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.
 
P

Peter Hansen

dean said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top