?
Solved

How to convert binary data into text?

Posted on 2011-04-26
31
Medium Priority
?
587 Views
Last Modified: 2012-05-11
Hi,

Could anyone please teach me how to convert binary data file into text file?

regards,
titanium
0
Comment
Question by:titanium0203
  • 8
  • 8
  • 5
  • +4
31 Comments
 
LVL 3

Expert Comment

by:Patricck
ID: 35465381
Hi,
just use this SW

http://www.gdargaud.net/Hack/BinToAscii.html

Regards
Patrik
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35465392
What do you mean by "binary data" ? Can you give an example of such "binary data", as well as an example of how you would like to have it in a text file ?
0
 

Author Comment

by:titanium0203
ID: 35465410
Hi,

Binary something like below :


ÅÇÝÔ%ÌuD ЦV2jA ÙÈÆ1q>ºc&Ÿ”Ï£ˆgT5 ®+˜†à‚-BÿSSï‚€q<]P§„4p¼¡      9¨ǸHÊ“¼ ([úE
D$¸á0SM;¨lv"íd²qÈÎ2AÛN¦U<Sˆé° *]`ê¤ IJ&ÌF‹â±5F† € @ò²iRÝ ÌP¨Xkþ3öfÒ#Ü›$o@˜Gðµ%¢ÍX0`‹$€ARš
…¥†

or do I need to convert it first to hexadecimal.

thanks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Infinity08
ID: 35465414
And what do you want the text file to contain ?
0
 
LVL 3

Expert Comment

by:Patricck
ID: 35465417
HI,

this is not binary :):):)

Binary is 10001110101000111000 ;-)
0
 

Author Comment

by:titanium0203
ID: 35465431
Hi,

In that case how to convert it?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35465443
Please see my question in http:#35465414
0
 

Author Comment

by:titanium0203
ID: 35465451
Hi,

ASCII character. I'm able to open it using HxD, but then how can I convert it to
ASCII character.

Thanks.
0
 

Author Comment

by:titanium0203
ID: 35465457
Hi

Added to my comment,

ASCII decimal format.

thanks
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35465525
>> ASCII decimal format.

You want the extended ASCII decimal value for each of the bytes from the binary file ? eg. output like :

        63 245 167 153 20 ...

?
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35465559
First of all, you should decide, what you want to do with the converted binary file.
From whatever you have written till now, I feel that, you want to convert a binary file to ascii.
Just by looking at the ascii or binary values, you will not be able to make out anything.

So, before doing all this, you should tell us, what is this binary file, how it was formed, etc?
Did you do gcc to get an a.out file or used a windows compiler to get a .exe file or used fwrite() function to write some records into some file - a.out, .exe and the file you got by writing some record (struct) using fwrite() , are all binary files.
Even a .pdf, .doc, .xls, .bmp, .jpeg, .mpeg, .ps, etc are all binary files.
If you know what file it is or how the file was formed, then you can decide about how to read the file or extract relevant data.
0
 

Expert Comment

by:gowts
ID: 35465856
Find out the endianness of your system. write a simple C program which will open this file read 1 byte as unsigned char format, then convert this into equivalent hex format using the ASCII to hex conversion routines. This is not a easy process but requires some manipulation of data.
0
 
LVL 35

Expert Comment

by:sarabande
ID: 35466733
you would open the file like

std::ifstream binfile("mybinfile.dat", std::ios_base::in | std::ios_base::binary);

then you could read it byte by byte (read it into buffers would be better later) like

unsigned char byte;
while (binfile.get(char&)byte))
{
    // std::cout << (int)byte << ' ';   // print it as decimal 
    std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)byte << ' ';  // print it as hex
}

Open in new window


instead to cout you also could print it to a textfile.

the hex format has the advantage that the output has fixed length.

Sara
0
 

Author Comment

by:titanium0203
ID: 35471847
Hi sarabande,

Do you have a complete solution to this issue as I'm still new in C programing?
Your help on this is really appreciated.

thanks.

Hi ssnkumar,

Actually the binary file contains product manufacture number of company abc, they
convert it to binary for some security reason and I need to convert it back to check
whether their manufacture number sync with the one that we have in our database.

Your help is really appreciated.
thanks.

Hi Infinity08,

Yes.

Thanks
0
 
LVL 85

Expert Comment

by:ozo
ID: 35472009
So do you need to convert the number to the same format at you have in your database?
To do that we would need to know, in what format it is stored in binary, and i what format it is stored in your database.
0
 

Author Comment

by:titanium0203
ID: 35472048
Hi ozo,

Yes, Can you name a few format? Are there any indicators that I can use to check which format there used in binary?

Thanks
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35472816
titanium0203, as I mentioned before, even you can just use fwrite() and write an integer to a file. That also looks like a binary data.
So, to read the data correctly, we will have to know the way it is written in (or the format). Without that, there is no guarantee that, the data read is correct.
Suppose some data is encrypted. Without decrypting we cannot read it.
To decrypt it, we will have to know, how it is encrypted.
Similarly, in your case also, we will have to know the way it is written into the file.

Or, if you want help in both writing to file and reading back, we can suggest some suitable method.
0
 
LVL 85

Expert Comment

by:ozo
ID: 35472864
If the numbers are integers in a fixed range, then a common format might be n bytes of data, where 256^n >= range
But since there is no security in that, it might be encrypted if security was an issue.
If it was encrypted, there is practically no chance of decoding it unless you know the encryption key or person who wrote the encryption was incompetent.

If it was not encrypted, then if you have a sample of the binary data together with the integers that you know are in the data,
then we might be able to guess the format if you can show us a hex dump of the binary.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35472940
>> Hi Infinity08,
>> 
>> Yes.

Then you can simply read the file one byte at a time, and display the byte as an integer. Nothing special has to be done :

        unsigned char buf[1024];
        size_t cnt = 0;
        while (0 < (cnt = fread(buf, 1024, sizeof(unsigned char), fp))) {
            size_t i = 0;
            for (i = 0; i < cnt; ++i) {
                printf("%d ", (int) buf[ i ]);
            }
        }


However, I get the feeling that that's not exactly what you need. Given what you said here :

>> Actually the binary file contains product manufacture number of company abc, they
>> convert it to binary for some security reason and I need to convert it back

It seems you'll need to know exactly HOW they encoded the data into the file. Do you have any documentation for that ?
0
 
LVL 35

Expert Comment

by:sarabande
ID: 35473340
when you look at the data with an hex editor you normally see the data at the left side in hex and at the right side in printable letters (if there are printable letters) like

00 00 00 30 31 41 42 35 01 10 20 20 0D 0A 43 44    ...01AB5..  ..CD

Open in new window


the period . normally serves as substitute for non-printable characters. you now could look on the right side with the printable characters if you see sequences like the 'manufacture number' you were looking for. if not, those numbers were encrypted and you have little chance to decrypt them without professional help or knowledge of the encryption algorithm.

Sara
0
 

Author Comment

by:titanium0203
ID: 35752437
Hi

Image below is my sample data.


data.bmp
0
 

Author Comment

by:titanium0203
ID: 35752459
Hi,

And I already confirm with my other team no encryption in the binary data. Hope you guys can
give me a complete solution on this.

I just need to apps that can :
1. read binary data form txt files
2. Output data in more human readable format.

Thanks
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35752489
Did you have a look at my post http:#35472940 ? It does what you want, following your comment http:#35471847. If it does NOT do what you want, then clearly describe how you want the textual output to be.
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35752701
> 1. read binary data form txt files
Do fstat() of the file and find the size of file. store it in size_of_file.
Open the file using fopen() and read the file using fread().
FILE *fp = fopen("data.bmp", "rb");
char *buf = (char *)malloc(size_of_file);
fread(buf, sizeof(char), sizeof(buf), fp);

Open in new window


fread() returns the number of characters read.
Call fread() in a while/for loop, till the total size read is less than the size of file you got in fstat().

> 2. Output data in more human readable format.
Use printf() and display the data in hex format.
for (i = 0; i < size_of_file; i++) {
    printf("%02x ", buf[i]);
}

Open in new window

0
 
LVL 35

Accepted Solution

by:
sarabande earned 200 total points
ID: 35753109
after open you code do read records like
    unsigned char buf[512] = { 0 };
    int n = sizeof(buf);
    while (binfile.read((char*)buf, sizeof(buf)) || (binfile.eof() && (n = binfile.gcount()) > 0))
    {

Open in new window

       

a hex output of such a record would be like

        for (int i = 0; i < n; i += 16)
        {
            int j;
            for (j = i; j < i+16; j++)
                std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)buf[j] << ' ';  // print it as hex
            std::cout << "| ";
            for (j = i; j < i+16 && j < n; j++)
                 if (isprint( (int)buf[j])) std::cout <<(char)buf[j];
                 else std::cout << ' ';
            std::cout << std::endl;

Open in new window

       }

after that you would make the buffer all zeros and call std::getline(std::cin, str); to let the user actively continue or break.

Sara
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35753158
I thought, we are in C zone and giving suggestions for writing C code.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35815031
@titanium0203 : could you explain why you chose a C++ solution for a question posted in the C zone ? Did you really want an answer for C++ ? If so, it would have been good to clarify that from the beginning, so we wouldn't have wasted all that time proposing things for C.

Could you also explain why none of the other posts helped you ? For example, my post http:#35472940 contains C code that does exactly what you asked for.
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35815237
I am surprised to see that a C++ solution is accepted in the C zone!!
0
 
LVL 35

Expert Comment

by:sarabande
ID: 35815546
i got the question from Microsoft Visual C++.NET topic area.

Sara
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35815748
"Microsoft Visual C++.NET" is a zone that refers to the IDE used. That IDE happens to support both C and C++, as well as many Microsoft specific extensions, but the language zone was "C Programming Language".
In any case, as far as I understand from http:#35471847, a C solution was asked for ...
0
 
LVL 35

Expert Comment

by:sarabande
ID: 35817131
when using the Visual C++.NET IDE the default language is c++. you need to change the default settings and have to start from an empty project to create pure c projects with the ide.

i recognized the c topic area after i already posted c++ code and titanium0203 seemed to be ok with it ?

Sara
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

840 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