# Thread: multiple binary files to ascii

1. The data did look like that, it was the same range of numbers over and over and over. Was wondering what it was. Did you ever get your program?

You pretty good with math, I have a post I need help with. I may be able to write you a program pretty quick in visual basic, but I need to know the format you want. Comma Delimited text file so you can parse it, or an XML file
http://www.mathhelpforum.com/math-he...t-pallets.html

2. I would not worry about spending too much time on this problem mate - I dont think the OP has even visited the forum since I posted my solution but hey I learned a little about binary. The output format was tab delimited in case you are keen tho.

I will have another look at your problem. I am in the process of moving so do not have easy access to the net at the moment. Maybe if you would like to find something similar to what you are trying to achieve (is there similar algorithms on the net?) then someone might be able to offer you a faster and more robust solution.

Regards Elbarto

3. DearElbarto,

I lost the contact with the forum for quite some time. Thanks for your kind reply and the Python decoder. It seems to work but the format is a problem. When I open the output text file in Excel I can't get the numbers in Real (not integers) aligned as a table of 180 columns and 90 rows. The output only gives a long row of integer values.
As I'm not familiar with Python and if you are still around,I would appreciate your help or anybody else.
Thanks
Zar

4. Dear Jkirkerx,

The values are a series of pressure data(180 columns * 90 rows), some may be repetitive. I use Fortran to decode these. My problem is that I wanted to decode many files automatically rather than running the program for every file

Thanks
Zar

5. Zar - you are correct my code will only return a single row as is but if you know how many columns you are expecting this is pretty trivial as printing a \n character every 180 iterations will produce the format desired. I will have a go tonight or tommorow to recitfy this issue.

As for not reading the numbers as floats - I will have to look into this as I am not familiar with binary at all. I suspect the problem might be the "'%0.0f" statment in my code that should maybe just be "%f". In other words it is being read correctly just not printed to any decimal places in my code.

When I get 15 mins I will post a new solution up.

Regards Elbarto

6. Dear Elbarto,

It's really impressive to have you so promptly. Thank you very much for that. I'm almost there with your hint. I can read the data normally as "real" and now in columns using:
fout.write('%6.5f\t\n' % val)

But still have to figure out how to have the 180 columns.

Thanks
Zar

7. This should be work I think. Are you expecting numbers after the decimal point? I only see trailing zeros.

Code:
import os
import struct
def convertFile(fname_in, fname_out):
NUM_COLS = 180
fin = open(fname_in,'rb')
fout = open(fname_out,'w')
isEOF = False
while not isEOF:
for i in range(NUM_COLS):
try:
fout.write('%f\t' % val)
except:
isEOF = True
break
fout.write('\n')
fin.close()
fout.close()
num_converted = 0
for fname in os.listdir(os.getcwd()):
if os.path.isdir(fname):
continue
else:
try:
f,ext = fname.rsplit('.',1)
if ext.lower() == 'bin':
convertFile(fname,f + '.txt')
print(fname + " converted to " + f + ".txt")
num_converted += 1
except ValueError:
pass
print(str(num_converted) + " file(s) converted...")
raw_input("Press Enter to Exit...")
Its a bit of an ugly code that im sure could be writen much simpler but its just the first thing I could bash out.
Regards Elbarto

8. Dear Elbarto,

Many thanks for all. You've been really fast. The program works well. The zeros are normal. The result is absolutely what I expected. Great.

You've been very helpful indeed and I appreciate that very much
Thanks a lot
Zar

9. No problem Zar - python is a nice language for those sort of problems (and many more). There was probably a easy way to modify your existing code but you dont see too much FORTRAN on the forums.

By the way that code is released as beerware
Beerware - Wikipedia, the free encyclopedia