convert assembly data (double words and bytes) in two tupple

J

joblack

I have two assembly data txt files, one has the structure:

0E1459D1Fh, 0AB58FAAEh, 4303E35Bh, 55FA3020h, 0E66D76ADh,
0EF434544h, ...

and the other has the structure:

53h, 6, 6Bh, 0D4h, 40h, 35h, 0B5h, 33h, 0AFh, 30h, 0B3h,
66h, ...

(I removed the dd and db with awk)

Now I want both of them in a tupple (like map1 = ( ... ) and map2 =
( ...) in my source code so I can index them..

Any idea how to give python a hint that the h at the end of the number
means it's a hex number? Or do I have to torture myself with regex to
get it right?
 
C

Chris Rebert

I have two assembly data txt files, one has the structure:

0E1459D1Fh, 0AB58FAAEh, 4303E35Bh, 55FA3020h, 0E66D76ADh,
0EF434544h, ...

and the other has the structure:

53h, 6, 6Bh, 0D4h, 40h, 35h, 0B5h, 33h, 0AFh, 30h, 0B3h,
66h, ...

(I removed the dd and db with awk)

Now I want both of them in a tupple (like map1 = ( ... ) and map2 =
( ...) in my source code so I can index them..

Any idea how to give python a hint that the h at the end of the number
means it's a hex number? Or do I have to torture myself with regex to
get it right?

No regex necessary whatsoever:

# will be inefficient if file is huge
f = open("path/to/assembly/file/1")
content = f.read()
f.close()
map1 = [int(hexnum[:-1], 16) for hexnum content.split(", ")]
# do same thing for 2nd file

Cheers,
Chris
 
M

MRAB

I have two assembly data txt files, one has the structure:

0E1459D1Fh, 0AB58FAAEh, 4303E35Bh, 55FA3020h, 0E66D76ADh,
0EF434544h, ...

and the other has the structure:

53h, 6, 6Bh, 0D4h, 40h, 35h, 0B5h, 33h, 0AFh, 30h, 0B3h,
66h, ...

(I removed the dd and db with awk)

Now I want both of them in a tupple (like map1 = ( ... ) and map2 =
( ...) in my source code so I can index them..

Any idea how to give python a hint that the h at the end of the number
means it's a hex number? Or do I have to torture myself with regex to
get it right?

If you insist on not using regex :)-)) then:
>>> text = "53h, 6, 6Bh, 0D4h, 40h, 35h, 0B5h, 33h, 0AFh, 30h, 0B3h, 66h"
>>> data = [t.strip() for t in text.split(",")]
>>> data
['53h', '6', '6Bh', '0D4h', '40h', '35h', '0B5h', '33h', '0AFh', '30h',
'0B3h', '66h']
>>> data = [int(d[ : -1], 16) if d[-1] == "h" else int(d) for d in data]
>>> data
[83, 6, 107, 212, 64, 53, 181, 51, 175, 48, 179, 102]
 

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

Forum statistics

Threads
474,167
Messages
2,570,910
Members
47,453
Latest member
MadelinePh

Latest Threads

Top