Jump to content
IGNORED

Python read ATR


Recommended Posts

Im trying to figure out a way to read an ATR file and get the Hex values.  Ive attached the ATR below but my python skills are a bit lacking.

 

I want to be able to handle the Hex values in the manner shown in HxD (ie, 00, 0A, 1C, etc...)

image.png

 

Right now in the Python debugger it shows the contents from the atr as:

image.png

 

I see Hex storage as \x02 but I cant seem to extract these values to convert them to the 00, 0A, etc.. format.

 

My goal is to get the Hex value to write out a new file as a string.

 

u4land.atr

Link to comment
Share on other sites

I don't do Phyton but perl.

 

Anyway, is that split OK? It seems that it should be something like:

xs = s.split('\\')

The colors from syntax highlighter from your debugger shows some unpairing there...

 

But, I also guess that the current values in the \x00 format is just for diplay only, it does not include the backslash. I think you have a byte stream (or string), and you should split into a char array (or byte array) probably using:

xs = s.split('')

Then, you need to convert each byte into a two digits hex value. In perl I'd use pack()/unpack() for conversions, but I don't know about Python.

 

Link to comment
Share on other sites

thanks ivop - I have some processing I need to do on the lines so I need it in an array.  Heres my final code to simply read and write the bytes I want for now - thanks for all the suggestions

 

number2 = []

file=open("u4land.atr","rb")
file1 = open("U4Sosaria.txt", "w")

for x in range (2063):
    number=list(file.read(16))

for x in range(63):
    number2.append(list(file.read(16)))

for i in range(63):
    outline = ""
    for x in range(15):
        outline += format(number2[i][x], '02X') + " "
    print(outline)
    file1.write(outline + "\n")

file.close()
file1.close()
  • Like 1
Link to comment
Share on other sites

18 minutes ago, Goochman said:

thanks ivop - I have some processing I need to do on the lines so I need it in an array.  Heres my final code to simply read and write the bytes I want for now - thanks for all the suggestions

 


number2 = []

file=open("u4land.atr","rb")
file1 = open("U4Sosaria.txt", "w")

for x in range (2063):
    number=list(file.read(16))

for x in range(63):
    number2.append(list(file.read(16)))

for i in range(63):
    outline = ""
    for x in range(15):
        outline += format(number2[i][x], '02X') + " "
    print(outline)
    file1.write(outline + "\n")

file.close()
file1.close()

It's always nice when you code something yourself and it does what you want! :)

 

The same can still be done with an od one-liner, though ;)

 

-j 12345     skip bytes

-N 12345    limit dump to 12345 bytes

-w16       16 hex values per line.

 

Edit: note that -w16 does not accept spaces between -w and 16. The other command line options do.

 

Edit2: full command line:

 

od -v -t x1 -A n -j $((2064*16)) -N $((64*16)) -w16 input > output

 

 

Edited by ivop
Link to comment
Share on other sites

I should explain more what Im doing.  I need to piece together every other line.  So bytes 1-16 need to have bytes 48-64 after them.  I first need to be able to get everything into a matrix and then rearrange them and write them out in the new format.  Thats why I need to write a program and not simply massage the output.

Link to comment
Share on other sites

10 minutes ago, Goochman said:

I should explain more what Im doing.  I need to piece together every other line.  So bytes 1-16 need to have bytes 48-64 after them.  I first need to be able to get everything into a matrix and then rearrange them and write them out in the new format.  Thats why I need to write a program and not simply massage the output.

I assume 48-64 means 49-64? I was not saying you shouldn't do what you are doing, just saying you can do a lot from the command line :)  What I see your Python code does is skip 2064*16 bytes, then print 64 lines of 16 hex bytes. Or is my Python so rusty? :D

 

My proposed command line does the same. Deleting the 17-32 and 33-48 lines could also be done in the same one-liner, for example change -w16 to -w64 and then run through cut(1).

 

The Unix command line is extremely powerful, if you know how to use it. If not, it's possibly faster to write a Python script :) My intention was in no way to demotivate or belittle your work.

 

Have fun!

Link to comment
Share on other sites

The code above just skips the first set of blocks and then reads in the data for straight output.  It doesnt have any logic - Im working on that now, but the numbers arent matching what Im seeing in HxD.  Its close but ugh this is a pain ;) 

Link to comment
Share on other sites

Thanks for everyones help and suggestions.  I finally got everything aligned and my code will produce the U4 Sosaria map from the atr.  The map is huge and stored in 16x16 chunks that need to be appended to each other left to right and top to bottom.

 

Hope this helps others breaking into atr's :)

(ignore style - I just wanted it to work :D )

 

number=[]
number2 = []

file=open("u4land.atr","rb")
file1 = open("U4Sosaria.txt", "w")

for x in range (129):
    number.append(list(file.read(16)))

for x in range(4096):
    number2.append(list(file.read(16)))

for o in range(16):
    yoffset = 0
    for i in range(16): # line
        outline = ""
        xoffset = (o * 256) + i
        for x in range(16): # column
                for y in range(16): #   row
                    outline += format(number2[xoffset][y], '02X') + " "
                xoffset+=16
        print(outline)
        file1.write(outline + "\n")

file.close()
file1.close()
  • Like 3
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...