How to convert binary data into text?

Hi,

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

regards,
titanium
titanium0203Asked:
Who is Participating?
 
sarabandeConnect With a Mentor Commented:
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
 
PatricckCommented:
Hi,
just use this SW

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

Regards
Patrik
0
 
Infinity08Commented:
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
titanium0203Author Commented:
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
 
Infinity08Commented:
And what do you want the text file to contain ?
0
 
PatricckCommented:
HI,

this is not binary :):):)

Binary is 10001110101000111000 ;-)
0
 
titanium0203Author Commented:
Hi,

In that case how to convert it?
0
 
Infinity08Commented:
Please see my question in http:#35465414
0
 
titanium0203Author Commented:
Hi,

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

Thanks.
0
 
titanium0203Author Commented:
Hi

Added to my comment,

ASCII decimal format.

thanks
0
 
Infinity08Commented:
>> 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
 
ssnkumarCommented:
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
 
gowtsCommented:
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
 
sarabandeCommented:
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
 
titanium0203Author Commented:
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
 
ozoCommented:
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
 
titanium0203Author Commented:
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
 
ssnkumarCommented:
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
 
ozoCommented:
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
 
Infinity08Commented:
>> 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
 
sarabandeCommented:
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
 
titanium0203Author Commented:
Hi

Image below is my sample data.


data.bmp
0
 
titanium0203Author Commented:
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
 
Infinity08Commented:
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
 
ssnkumarCommented:
> 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
 
ssnkumarCommented:
I thought, we are in C zone and giving suggestions for writing C code.
0
 
Infinity08Commented:
@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
 
ssnkumarCommented:
I am surprised to see that a C++ solution is accepted in the C zone!!
0
 
sarabandeCommented:
i got the question from Microsoft Visual C++.NET topic area.

Sara
0
 
Infinity08Commented:
"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
 
sarabandeCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.