2

I want to convert a byte array (containing 64 elements) which looks something like this:

9b b4 f5 b0 67 3c f8 e1 f1 f8 02 8c b2 13 4d 66 f0 72 a0 05 ...

to a string. Something like "9bb4f5b067.....". Essentially I want to write this byte array into a CFile and I realized the most easy way is to convert the byte array to a string and then write the content. Whenever I try to convert the array to a string I get some special characters and when I try to write the byte array directly to the file, I see special characters written in the file. Any suggestions please?

Here is the code: pbSignature is of type PBYTE and is printed like this:

printf("The signature is:  ");
for (DWORD i = 0 ; i < cbSignature ; i++) 
{
    printf("%2.2x ",pbSignature[i]);
}

// Imprint the signature to the CRC file
if (!file.Open(crcFilePath, CFile::modeWrite, NULL))
{
    printf("File could not be opened %d\n");
    goto Cleanup;
}

for (DWORD i = 0 ; i < cbSignature ; i++) 
{           
    //printf("size of pbsig is %d and value of pbsig is %d\n",sizeof(pbSignature[i]),pbSignature[i] );
    file.Write(&pbSignature[i],sizeof(pbSignature[i]));
    //printf("%2.2x ",pbSignature[i]);
}
3
  • You can write simple raw bytes to a binary file as well. Why do you want to convert the byte array to string? Do you want a string containing the hex representation of your byte array data? Commented Mar 21, 2014 at 10:58
  • SHow some code, otherwise it's hard to tell what's going on. Commented Mar 21, 2014 at 11:01
  • I tried that but I get special characters written in the file. I am using the following method: Imprint the signature to the CRC file if(!file.Open(crcFilePath, CFile::modeWrite, NULL)) { printf("File could not be opened %d\n"); goto Cleanup; } string str; for(DWORD i = 0 ; i < cbSignature ; i++) { str(*pbSignature[i]); //printf("size of pbsig is %d and value of pbsig is %d\n",sizeof(pbSignature[i]),pbSignature[i] ); file.Write(&pbSignature[i],sizeof(pbSignature[i])); //printf("%2.2x ",pbSignature[i]); } Commented Mar 21, 2014 at 11:02

3 Answers 3

2

You can simply directly write the byte array content as raw byte sequence to a binary file.
You can use the CFile::Write() method, e.g.

// 'file' is an instance of CFile, and is already opened for writing
file.Write(yourBufferPointer, yourBufferSize);

If you want to write the content of the byte array as a sequence of hex digits (considering the file as a text file), you can simply define a helper function to convert a BYTE into a string containing the byte hex representation. Then you can iterate through the input array, convert each byte to an hex string, and print the string content to the file.

This is an example of such a conversion function, printing hex byte sequence in console:

#include <stdio.h>      // For sprintf_s()
#include <iostream>     // For console output
#include <string>       // For std::string
#include <Windows.h>    // For Win32 Platform SDK (for BYTE definition)
using namespace std;

string byteToHexString(BYTE b) {
    char hexBuf[3]; // One byte in hex is 2 digits, plus NUL string terminator.
    sprintf_s(hexBuf, "%02X", b);
    return hexBuf;
}

int main() {
    BYTE buffer[] = {0x11, 0x22, 0x33, 0xAA, 0xBB, 0xFF};

    for (size_t i = 0; i < sizeof(buffer); ++i) {
        cout << byteToHexString(buffer[i]) << ' ';
    }
    cout << endl;
}

Compile and run test in console:

C:\Temp\CppTests>cl /EHsc /W4 /nologo test.cpp
test.cpp

C:\Temp\CppTests>test.exe
11 22 33 AA BB FF

If you want to convert the whole binary array into a single string at once, you can define a function like this (note that this is not good if the binary file is huge; in this case, it's better to follow the previous convert-single-bytes-and-write-each-byte-to-file approach):

string toHexString(const void* buffer, size_t size) {
    auto bytes = static_cast<const unsigned char*>(buffer);
    string result;
    for (size_t i = 0; i < size; ++i) {
        // One byte is represented in hex using 2 hex digits,
        // and consider also the NUL string terminator.
        // ---> total buffer size: 3 characters
        char hexBuf[3];
        sprintf_s(hexBuf, "%02X", bytes[i]);
        result += hexBuf; 
    }

    return result;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You probably want this:

unsigned char bytes[] = {0x9b, 0xb4, 0xf5, 0xb0 } ;   // your array of bytes
string st ;
for (int i = 0; i < sizeof(bytes)/sizeof(bytes[0]); i++)
{
   char buff[4];
   sprintf(buff, "%02x", (unsigned char)bytes[i]) ;
   st = st + buff ;
}
// now st contains "9bb4f5b0"

2 Comments

Yes. Thanks this seems to be the correct approach. However are you sure about the sizeof(bytes) in the for loop. Shouldn't it be the the number of elements in the byte array? Please correct me if I am wrong.
@user2695082: you are right sizeof(bytes) works here because 1 byte takes one byte. It should be sizeof(bytes)/sizeof(bytes[0]) which is the number of elements independent of the size of one element. Thanks for pointing this out.
0

Convert your byte array to a base64 string this will make it readable:

C++ Base64 Implementation

2 Comments

OP wants to print the byte values as string. Byte 9b printed as 9b. Base64 encoding just convert the bytes into 7 bits.
Agreed, however the base64 technique is easier to convert hence and forth in my opinion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.