Solved

How to convert a binary file into ASCII format?

Posted on 2006-11-16
4
7,783 Views
Last Modified: 2013-11-15
I have a binary file and I want the same to convert into ASCII format with symbols,i,e in a readable format.Please tell me the steps to follow....

1) How can I read a binary file which contain all 0's and 1's .
0
Comment
Question by:tatikor_143
  • 2
4 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 17957013
There are many ways this could be done. I suspect you have something specific in mind, or that you, at least, won't like some of the options. The first question is what do you want the output to look like. If you consider the following sequence of bytes in a file (shown in hexadecimal):

41 42 01 05 43 4F

This could be rendered in ASCII in a  number of ways.

1. Show the corresponding ASCII character:  A B (open face) (club) C O
    One drawback here is that lots of bytes don't correspond to well defined or printable characters.
    1 and 5 are often used as control codes.

2. Show the nibbles; that is, show each half byte (nibble) as a character from 0 to ?: 4 1 4 2 0 1 0 5 4 3 ?
    This ensures that everything is printable.

3. Show all the ones and zeroes:  01000001 01000010 00000001 00000101 01000011 01001111
    The drawback here is that the printout will be very long.

4. Show it in hex as above: 41 42 01 05 43 4F

To read a binary file under windows you open it in binary mode:

#include <stdio.h>
char buf[100];
FILE *fp;

fp=fopen("thefile","rb");
fread(buf,100,1,fp);

and you have the first 100 bytes in buf. Then you need to process them in some way to show them as you want to.
0
 
LVL 16

Expert Comment

by:imladris
ID: 17957031
To show those 100 bytes in hex you could do something simple like:

int i;

for(i=0; i<100; ++i)printf("%x ",buf[i]);
printf("\n");
0
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 17958159

Hi guys,

My guess is that the poster has a binary file such as a .exe, .jpg, .dbf, etc.  Formatting this is a snap.


You can pipe this into a program designed to format this or just open the file and read it.  Your choice.

To do the actual formatting, just use printf() or fprintf()


main ()  // read file from stdin
{
  char ch;
  int    bytes;

  bytes = 0;
  while (1)
  {
    ch = fgetc (stding)
    if (feof(stdin))
      break;

    fprintf (stdout, " %d> %2.2x %c\n", bytes, ch, ch < 0x80 && ch >= ' ' ? ch : ' ');
    ++bytes;
  }
}


This will print something like:

 0> 54 T
 1> 65 e
 2> 73 s
 3> 74 t
 4> 20


etc....

Good Luck,
Kent
0
 
LVL 22

Expert Comment

by:grg99
ID: 17959765
What's in the file?   characters?    numbers?    bytes, words, longs?   signed or unsigned?

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
Using Adobe Premiere Pro, the viewer will learn how to set up a sequence with proper settings, importing pictures, rendering, and exporting the finished product.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now