LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to convert binary file into text file in linux (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-convert-binary-file-into-text-file-in-linux-4175495937/)

ihoward92 02-23-2014 07:53 AM

How to convert binary file into text file in linux
 
Hi,

I am trying to run a fluid simulation in Scilab, creating a 2d velocity field. For this the simulation is being run using linux which creates a binary file of data. I need to convert this into a basic text file that you would use on windows notepad, the data when converted to a normal text file should show a matrix of numbers. Does anyone have any idea how this can be done? Cheers

jpollard 02-23-2014 08:31 AM

You will likely have to write a program that knows the format of the field data file, and prints in the format you want for windows.

Though why notepad I'm not sure - (not being familar with notepad) the file could be quite large...

jdkaye 02-23-2014 09:17 AM

Quote:

the simulation is being run using linux which creates a binary file of data
I'm not quite sure what you mean by "binary file of data"? What exact format is this "binary file" and what programs in linux are supposed to run it. Usually the program that runs the binary file should have an option of saving the data in a text file.
jdk

ihoward92 02-23-2014 09:47 AM

Here was the source code to dump the data into binary files, i need to instead of "write" read the binary file and convert it into a text file, will something like base64 work?
subroutine datadump
c
implicit none
include '../sttls.h'
include '../dcl.h'
c
integer*4 i,j,k,l
c
open (unit=2,status='unknown',file='results/y.dat',
> form='unformatted')
do i=1,nx
write(2) (((x(l,i,j,k),l=1,16),j=1,ny),k=1,nz)
write(2) ((fx(i,j,k),j=1,ny),k=1,nz)
write(2) ((fy(i,j,k),j=1,ny),k=1,nz)
write(2) ((fz(i,j,k),j=1,ny),k=1,nz)
write(2) ((c(1,i,j,k),j=1,ny),k=1,nz)
enddo
write(2) teta,nrev
close (unit=2)
end

jpollard 02-23-2014 09:55 AM

No.

And if you look at the data, it has varying sizes - ny, nz, and nk.

None of which are contained within the file... so the file itself is arbitrary.

Just converting it to a base64 representation won't make it readable by anything.

Now if the machine architectures are compatible (as in using IEEE floating point, and same endian form) then just moving the binary file is sufficient for some compatible program to read (maybe - not sure what Windows does for binary files).

What are you trying to do?

grail 02-23-2014 10:10 AM

I am curious, the original problem is worded such that because it was created in linux the file needs to be changed there to be read in Windows.

I put it to the OP, if the binary file had been created under Windows, how would you have converted the file then?

ihoward92 02-24-2014 09:19 AM

Basically i am running a simulation in linux which has about 20 sourcecode files. I am new to linux and dont really understand how the whole process works but i was told i need to get the binary data that the simulation produces and convert it into a text file that i have written a program for in Scilab to read the text file and produce a velocity vector graphic simulation. I have a couple of sourcecode files for dumping the data and writing the file. I also have a folder which contains various files which contains the binary data. Overall the text files should produce numerical matrices that Scilab can read. The problem is that when the simulation is running it takes timestamps of the simulation and creates a new file for the data so i have a bunch of files for each timestamp giving different data. A Folder called cs in linux has all the data in the folder which has various files named e.g 2426, 2427,2428 etc each in it has data at a particular time in the simulation in binary. Does that help? is there anymore information i could provide that would provide a more clear indication of the problem in terms of coding? Cheers

schneidz 02-24-2014 09:53 AM

i am not familiar with scilab so sorry if this is off-base, but it seems like what you are trying to do would be similar to converting an .mp3 audio file into ascii-text. (i know whenever i passed in my matlab assignments the code and output was always text unless there were a few plots i needed to print out).

i know in the case of things like open-office or microsoft word there is an option to file -> save as -> 'text' that can be read in vi or notepad.exe (does scilab have a similar export feature ?

looks like you written a program that is supposed to convert certain values to ascii text. how big is the output, i dont know scilab but can you upload a dump of the output somewhere and list what you would want as the expected output.txt (i suspect something can be written in 'c' to parse it).


edit: maybe hexedit or od would help here ?

suicidaleggroll 02-24-2014 10:17 AM

So you wrote a program (or are using a program) that writes some random format binary file.

Then you wrote a program that reads in some random format ASCII file.

And now you need a program to convert the random format binary file into a random format ASCII file? Why didn't you just write your data parser to read the original binary files instead of some random ASCII format that doesn't exist? Why can't you modify the code that's writing the binary files so that it writes ASCII instead in the format you want?

This whole situation makes no sense to me. And it has nothing at all to do with Linux vs Windows.

I guess the main question is, why did you write your Scilab program so that it reads in ASCII files that don't exist rather than the binary files that do?


To answer the original question, "How to convert binary file into text file in linux" - you write a custom program to parse the binary file and write out the data in ASCII, same as you would in Windows.

Smokey_justme 02-24-2014 10:41 AM

I don't think Linux or operating systems are your problem here.. Rather you need someone that's familiar to Scilab... Please stop involving operating systems here since they have nothing to do with your problem..

From what I gather.. You need to create your own program that reads that specific file format (which happens to be in a binary form) and convert it to plain text (it will need to even respect your pattern/format so it can be usable in your other Scilab program)... I can assure you that no one else can do this for you..

John VV 02-24-2014 03:15 PM

Quote:

I need to convert this into a basic text file that you would use on windows notepad,
that will be a problem
even a "copy/ paste " of this post i am writing on linux and reading it on MS's notepad
will be "fun"

you NEED !!!! to run "unix2dos " on it

but do your self a VERY BIG favor on windows
UNINSTALL NOTEPAD.EXE


and use say "SciTE" or the Scite based program "notepad++"

as to data from the scilab code you wrote
just hack your code to output as a ascii CSV
or
to output the binary data into a organized output
say a new line for every value


All times are GMT -5. The time now is 01:07 PM.