LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-23-2011, 07:02 AM   #1
scream
Member
 
Registered: Feb 2008
Distribution: Oracle Linux
Posts: 32

Rep: Reputation: 0
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?
 
Old 05-23-2011, 08:53 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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
 
Old 05-23-2011, 02:30 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,028

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
there is a strings command that looks at binary files and prints out lists of 3 or more consecutive ascii chars.
 
Old 05-24-2011, 12:29 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
google results for bash parse binary file

first hit might get you on the way
http://stackoverflow.com/questions/1...ad-binary-file
 
Old 05-24-2011, 01:33 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,028

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
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);
}
 
Old 05-24-2011, 08:59 PM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by schneidz View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert a hex file to an ASCII file? lxnbie Linux - Server 1 09-08-2010 04:49 AM
Need to convert a batch file to a shell script webshore Linux - Newbie 2 05-03-2010 08:42 AM
[SOLVED] How to convert binary file to ascii file? idaham Linux - Newbie 9 09-18-2009 04:19 AM
Editing Ascii File Using Shell Script rohit2983 Programming 7 01-03-2007 07:27 AM
how to convert a text file into shell script meng_en Linux - General 9 10-15-2002 11:46 PM


All times are GMT -5. The time now is 06:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration