Goochman Posted October 5, 2021 Share Posted October 5, 2021 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...) Right now in the Python debugger it shows the contents from the atr as: 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 Quote Link to comment Share on other sites More sharing options...
vitoco Posted October 5, 2021 Share Posted October 5, 2021 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. Quote Link to comment Share on other sites More sharing options...
Goochman Posted October 5, 2021 Author Share Posted October 5, 2021 Thanks vitoco - I think you are on the right track. The \x00 display is from the debugger and I dont see a way for me to get those numbers/format. I prob need to unpack or open the file in a different manner to get the bytes Quote Link to comment Share on other sites More sharing options...
TGB1718 Posted October 5, 2021 Share Posted October 5, 2021 Again, I don't know much Python, but your using a function "Readlines", the file is not text so I don't think you can read it that way, I think you should be opening it for "binary" read and get the file a byte at a time. Quote Link to comment Share on other sites More sharing options...
Goochman Posted October 5, 2021 Author Share Posted October 5, 2021 Thanks for the input - I found the way to read "bytes" from a file and thats got me going in the right direction file=open("u4land.atr","rb") number=list(file.read(16)) #Gets the first 16 bytes into a list print (number) file.close() Quote Link to comment Share on other sites More sharing options...
ivop Posted October 5, 2021 Share Posted October 5, 2021 On the command line: $ od -v -t x1 -A n input > output -v do not use * to mark line suppression -t x1 --> type is 1 byte hex -A n ---> no address lines. man od Quote Link to comment Share on other sites More sharing options...
Goochman Posted October 5, 2021 Author Share Posted October 5, 2021 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() 1 Quote Link to comment Share on other sites More sharing options...
ivop Posted October 5, 2021 Share Posted October 5, 2021 (edited) 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 October 5, 2021 by ivop Quote Link to comment Share on other sites More sharing options...
Goochman Posted October 5, 2021 Author Share Posted October 5, 2021 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. Quote Link to comment Share on other sites More sharing options...
ivop Posted October 5, 2021 Share Posted October 5, 2021 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? 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! Quote Link to comment Share on other sites More sharing options...
Goochman Posted October 5, 2021 Author Share Posted October 5, 2021 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 Quote Link to comment Share on other sites More sharing options...
Goochman Posted October 5, 2021 Author Share Posted October 5, 2021 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 ) 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() 3 Quote Link to comment Share on other sites More sharing options...
ivop Posted October 5, 2021 Share Posted October 5, 2021 (edited) One note on style though, never use o or O or 0 as a variable name Same goes for l Edited October 5, 2021 by ivop Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.