I have a binary file that need to be showed in hexadecimal. The code as follows :
file=open('myfile.chn','rb')
for x in file:
i=0
while i<len(x):
print ("%0.2X"%(x[i]))
i=i+1
if (i>=10):
i=0
break
file.close()
and the result i get as follows :
FF
FF
01
00
01
00
35
36
49
EC
.
.
.
Which part of the code that i need to change in order to show the result just as follows?
FF FF 01 00 01
00 35 36 49 EC
.
.
(with a space between each byte)
As you take only 10 elements I’d use:
or if you want to include the whole line:
There is still a bug from your initial version. Your input is read as one string per line. The type conversion “%0.2X” fails (“%s” works). I think you cannot read binary file per line. \n is just another byte and cannot be interpreted as newline.
When you have a sequence of int values you can create partitions of n elements with the group method. The group method is in the itertools recipies.
Output:
To bytes_from_file reads bytes as generator :