Hi everyone.. Me again.
I am confused about something so please correct me if I am not making sense.

I have a program which outputs binary output which has been read from a file of encrypted data.
When de-encrypted the output is supposed to represent numbers.

Because it comes out in binary format, viewing it through a hex editor the correct output is revealed. But... when I convert each byte to an int, it comes out with different decimal output to what it is supposed to. Obviously it will interpret the spaces and line feeds as well, but none of the correct data is there once it has been converted to visible decimal output.

How do I convert binary data to decimal output correctly in C++?

I have tried the below.

stringstream mystream;
    int myint;
    mystream << buffer; (this is a char buffer)
    mystream >> myint;
    cout << myint; // give me all zeros.


    I have also tried.

    myint = atoi(buffer);
    cout << myint; // still give me zeros.

Any clues?

Edited 2 Years Ago by christinetom

The file data is decoded jumble. I can only see the correct values via a hex editor. They are correct as to what I was expecting to see.

I just want to turn them into visible numbers.
You would think that any method which converts
Hex to int would work, but this is the point where all the values come out as Zeros.

There must be a common principle for converting hext to int. The ones I have mentioned don't work but there could be a reason for it. I cannot post the data unfortunately.

020010022350032500560 ? // this is something like what I expect to see.

Is this what you mean. I'm just interested in turning each hex (byte) into decimals.

More clear now. Iterate through the string and with each 2 characters, convert to decimal using the strtol function, specifiying the base option as 16 (hex). That will give you the (signed) long value of the 2 bytes that you want. RTMP (read the man page) for strtol().

Hi.Thanks for the reply but strtol doesn't seem to be giving me the output I need. Basically I have output from a decoded file stream and have placed it in the attached text file. Don't save it, it's been written to in binary mode. I can read the correct values via a hex editor. Please see that output (from the hex editor below). The below is as how it is supposed to be.

00 00 13 00 02 00 2E 01 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 01 04 90 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 02 00 2D 00 02 00 2D 01 02 00 
2C 00 02 00 2B 00 00 00 00 00 01 13 23 00 02 00 2E 00 
00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
02 00 2F 01 02 00 2F 00 02 00 2F 02 02 00 2F 04 02 00 
2F 03 02 00 2F 05 02 00 2F 06 01 13 73 00 01 14 EA 00 
01 47 5A 00 01 4E 9E 00 01 4F 49 00 01 50 26 00 01 52 
BA 00 01 55 E8 00 01 9E 78 00 01 00 10 00 01 01 0E 00 
01 01 A1 00 01 02 2B 00 01 03 5A 00 01 A5 BC 00 

I have then tried to convert these to integars so I can see the values as decimals but it's proving impossible. You would think a simple conversion of each byte to int would do it, but the fact that I am reading the correct output in hex means that I would need to read each nibble and convert it to hex. That won't work either. I am trying to produce the decimal equivalent to the hex values above. Any clues as to how I should go about this.

Thanks

Attachments
00 00 13 00 02 00 2E 01 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 01 04 90 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 02 00 2D 00 02 00 2D 01 02 00 
2C 00 02 00 2B 00 00 00 00 00 01 13 23 00 02 00 2E 00 
00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
02 00 2F 01 02 00 2F 00 02 00 2F 02 02 00 2F 04 02 00 
2F 03 02 00 2F 05 02 00 2F 06 01 13 73 00 01 14 EA 00 
01 47 5A 00 01 4E 9E 00 01 4F 49 00 01 50 26 00 01 52 
BA 00 01 55 E8 00 01 9E 78 00 01 00 10 00 01 01 0E 00 
01 01 A1 00 01 02 2B 00 01 03 5A 00 01 A5 BC 00

Woops, the binary output file I uploaded was teh wrong one. I am trying to upload the correct file but I can't.

msg:

'The filetype you are attempting to upload is not allowed.'

Edited 2 Years Ago by christinetom

When I go to upload it, it registers as "All Files" . It is a text file which has had binary data read to it.

Well it appears that I had the high and low nibbles around the wrong way. The below works fine and displays the values in hex.

int main()
{
    ifstream myfile;
    myfile.open("outfile.txt", ios::out | ios::binary);

    char *in = new char;

    while (myfile.read(in,1))
    {
        unsigned char lo = *in & 0x0f;
        unsigned char hi = (*in >> 4) & 0x0f;

    int los = lo;
    int his = hi;
    cout  << hex << his << los << "  ";


    }

Edited 2 Years Ago by christinetom

This article has been dead for over six months. Start a new discussion instead.