literal hex value to bin file.

Y

yaipa h.

All,

Sorry if this has been asked before, doing a search on c.l.p, I didn't
find this particular use case.

I wish to use the struct module, in windows, to pack hexdigits in such
a way that when I pack, say 0xa into a packed object, I can then use
it to find 0xa in a binary file.

Likewise, I wish to build a hex sequence from an Assembly language
..lst (text)file so that I can search for that sequence of hex digits
in a binary file.

Thanks,

--alan
 
P

Peter L Hansen

yaipa said:
Sorry if this has been asked before, doing a search on c.l.p, I didn't
find this particular use case.

I wish to use the struct module, in windows, to pack hexdigits in such
a way that when I pack, say 0xa into a packed object, I can then use
it to find 0xa in a binary file.

The question is unclear to me, but wouldn't just using "\x0a"
build a string (i.e. a 'byte sequence') with identical contents?
A "packed object" from struct is merely a string.
Likewise, I wish to build a hex sequence from an Assembly language
.lst (text)file so that I can search for that sequence of hex digits
in a binary file.

Unclear. Please provide examples of before and after cases
of each thing mentioned above to help guide us...

-Peter
 
Y

yaipa h.

Peter L Hansen said:
The question is unclear to me, but wouldn't just using "\x0a"
build a string (i.e. a 'byte sequence') with identical contents?
A "packed object" from struct is merely a string.


Unclear. Please provide examples of before and after cases
of each thing mentioned above to help guide us...

-Peter

Peter,

Thanks.

I seemed to have worked it out. The bit of code below will write the
actual
hex string literal to a binary file as is. What I kept getting was
the hexascii representation of the hex string, so that '9' would write
to file as 0x39 and 'a' would write to the file as 0x61. What I wanted
was '9' to write to the file as 0x09 and '7f' to write out as 0x7f.

#-----------------------------------------------
import struct

a = struct.pack('B', int('ff', 16))

fh = open("a.hex", "wb")
fh.write(a)
fh.close()
#-----------------------------------------------
# results in the binary file 'a.hex' containing,
0xff # only

Peter, thanks again for your attempt to understand my confusion.

Cheers,

--Alan
 
P

Paul Rubin

I seemed to have worked it out. The bit of code below will write
the actual hex string literal to a binary file as is. What I kept
getting was the hexascii representation of the hex string, so that
'9' would write to file as 0x39 and 'a' would write to the file as
0x61. What I wanted was '9' to write to the file as 0x09 and '7f' to
write out as 0x7f.

Oh I see. Normally you'd use the chr function:
i = int('7f', 16)
fh.write (chr(i))
 
P

Peter L Hansen

yaipa said:
I seemed to have worked it out. The bit of code below will write the
actual
hex string literal to a binary file as is. What I kept getting was
the hexascii representation of the hex string, so that '9' would write
to file as 0x39 and 'a' would write to the file as 0x61. What I wanted
was '9' to write to the file as 0x09 and '7f' to write out as 0x7f.

#-----------------------------------------------
import struct

a = struct.pack('B', int('ff', 16))

fh = open("a.hex", "wb")
fh.write(a)
fh.close()
#-----------------------------------------------
# results in the binary file 'a.hex' containing,
0xff # only

Yep, you've got it. As Paul suggests, chr() works nicely
for a single byte though.

Also investigate the binascii module, specifically the
hexlify() and unhexlify() methods, which work nicely on
entire strings of 2-digit hex values:
'\t\x7f'

All of those are of course the same... depends on your
needs.

(Note that the output is the repr() value of the string,
which in this case contains an ASCII 9 and 7F, thus the
\t or TAB escape sequence followed by the hex escape
sequence representation of the 7F byte. Sometimes the
fact that the interactive interpreter always does a repr()
on the output of expressions can be confusing.)

-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
474,209
Messages
2,571,088
Members
47,684
Latest member
sparada

Latest Threads

Top