Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

convert from binary to text

Posted on 1999-07-07
9
Medium Priority
?
2,409 Views
Last Modified: 2013-11-15
hai ,
 could anyone suggest me how to convert binary file to text file without loosing information.
is theire any function in C to convert each byte to ASCII format.
i want load all binary file in to a buffer and convert it into text format.
0
Comment
Question by:hmk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 6

Expert Comment

by:Triskelion
ID: 1263379
UUENCODE
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 1263380
/*
start with a file called file1.bin that contains your binary data.  Run this program over that file and a new file called fiile2.txt will be created containing an ascii representation of the old file.  Then a new file called file3.txt will be created containg by converting the text back to binary.
*/
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char BYTE;

void main(void)
{
      FILE      *      hInFile;
      FILE      *      hOutFile;
      BYTE            byteData;
      BYTE            byteArray[3];
            
      if (NULL == (hInFile=fopen("file1.bin", "rb")))
            {
            puts("cannot open file1.bin");
            exit(1);
            }
      if (NULL == (hOutFile=fopen("file2.txt", "wb")))
            {
            puts("cannot open file2.txt");
            exit(2);
            }
      while(!feof(hInFile))
            {
            fread(&byteData, 1, sizeof(byteData), hInFile);
            if (!feof(hInFile))
                  {
                  fprintf(hOutFile, "%03d", byteData);
                  }
            }
      fclose(hOutFile);
      fclose(hInFile);
      /* put it back*/
      if (NULL == (hInFile=fopen("file2.txt", "rb")))
            {
            puts("cannot open file2.txt for read access");
            exit(3);
            }
      if (NULL == (hOutFile=fopen("file3.bin", "wb")))
            {
            puts("cannot open file3.bin for write access");
            exit(4);
            }
      while(!feof(hInFile))
            {
            static char spTempString[4];
            fread(&byteArray, 1, sizeof(byteArray), hInFile);
            
            if (!feof(hInFile))
                  {
                  memcpy(&spTempString, &byteArray, 3);
                  spTempString[3]='\0';
                  fprintf(hOutFile, "%c", atoi(spTempString));
                  }
            }
      fclose(hOutFile);
      fclose(hInFile);
}
0
 
LVL 2

Expert Comment

by:Ready4Dis
ID: 1263381
I have some code also.
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

Author Comment

by:hmk
ID: 1263382
hai triskelion,

i am sorry i mislead the question, what i want is to convert binary file in to text file with actual alphanumeric charecters as a text not in ASCII format txt file.
This binary file is actually a Btrieve file consists spaces,Control breaks,charecters and........
could u send me how to convert this btrieve file in to a text file.
The previous program was worked fine.
hope u find the solution,
Thankyou,
hmk.


0
 
LVL 6

Expert Comment

by:Triskelion
ID: 1263383
What will you do with the non-printable characters?
1) What version of BTrieve?
2) Do you have the BTrieve driver loaded?
3) If >= version 5, can you use C++ and ODBC?
If you want to just dump the contents into a text file, you will need to either use the BTrieve library with proper calls, read the data with the proper lookup function and fprintf the data into your output text file.  If you do not have the driver and the version is < 5, the you will have to examine the file layout and create your own routines that do the lookups.
0
 
LVL 2

Accepted Solution

by:
hougaard earned 100 total points
ID: 1263384
X = "your binary char"

A = (X & 11110000 >> 4) + 65;
B = (X & 1111) + 65;

A and B are two ASCII charaters

Y = (A - 65) << 4 + (B - 65);  // To go back to binary


0
 
LVL 6

Expert Comment

by:Triskelion
ID: 1263385
If you're asking if you have a value like a long
that you have read in (in binary form) and you want to save it in a text compatible file, you can have something like this

{
   auto long lCustNum;
   auto int  iValue;
//Open file up here
//Read in data in binary format
...
   fprintf(hOutFile, "%ld %d", lData, iValue);//Save data in text form
...
//close file down here
}
0
 

Expert Comment

by:prabhuram
ID: 1263386
All are kotting
0
 

Author Comment

by:hmk
ID: 1263387
sorry ,please delete the question none of above solutions work..
Thanks for your valuable advice


hmk
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
Want to know how to use Exchange Server Eseutil command? Go through this article as it gives you the know-how.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
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…

705 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