LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Convert binary file in to ascii file using shell script (https://www.linuxquestions.org/questions/linux-newbie-8/convert-binary-file-in-to-ascii-file-using-shell-script-882227/)

scream 05-23-2011 06:02 AM

Convert binary file in to ascii file using shell script
 
Hi,
i am trying to convert a binary file in to ASCII using shell script. this file contains multiple types of data like string, number, bcd, etc. can any one guide me how to do that?

Snark1994 05-23-2011 07:53 AM

I think either you or I don't understand the issue here... ASCII in many ways is binary - it's a way of assigning letters to binary numbers (or in fact numbers in general). So you can probably view the data in the file by using a hexeditor, but to "convert" it to ASCII doesn't make much sense to me.

However, perhaps you could provide us with an example of some binary and the ASCII you would like to convert it into, so we can see more precisely what you want :)

schneidz 05-23-2011 01:30 PM

there is a strings command that looks at binary files and prints out lists of 3 or more consecutive ascii chars.

Wim Sturkenboom 05-23-2011 11:29 PM

google results for bash parse binary file

first hit might get you on the way
http://stackoverflow.com/questions/1...ad-binary-file

schneidz 05-24-2011 12:33 PM

not sure if this is what you want but this code reads a file char-by-char and if it is not a printable ascii letter, it will substitute it as a space:
Code:

#include "stdio.h"
 
main(int argc, char *argv[])
{
 int c;
 FILE * fstream;
 fstream = fopen(argv[1], "r");
 c = fgetc(fstream);
 while(c != EOF)
 {
  if(c == 10)
  printf("%c", c);
  if((c >= 0 && c <= 9) || (c >= 11 && c <= 31))
  printf(" ");
  if(c >= 127)
  printf(" ");
  if(c >= 32 && c <= 126)
  printf("%c", c);
  c = fgetc(fstream);
 }
 fclose(fstream);
}


Wim Sturkenboom 05-24-2011 07:59 PM

Quote:

Originally Posted by schneidz (Post 4365799)
not sure if this is what you want but this code reads a file char-by-char and if it is not a printable ascii letter, it will substitute it as a space:

Code:

pile of C code

Shell script ;) See title of post :)


All times are GMT -5. The time now is 12:53 AM.