If I got you, the code should return the hexadecimal representation of a bunch of bytes. In order to avoid CRT-conflicts, you have to make the caller responsible of the output buffer allocation/deallocation. Something like this
#include #include using namespace std;
// in [INPUT] - the binary array we wish to represent with an hex string
// size_in [INPUT] - the size (bytes) of the binary array
// out [OUTPUT] - the buffer receiving the hexadecimal representation of the binary data
// out_size [INPUT] - the size of the buffer. It should be at least (size_in*2+1) in order ot represent all the input data
size_t to_hex( unsigned char in[], size_t size_in, char out[], size_t size_out)
{
size_t n;
for (n=0; (n < size_in) && (n < (size_out-1)b/2); ++n)
{
unsigned char nibble;
nibble = (in[n] >> 4); // the MSB nibble
out[2*n] = nibble < 10 ? nibble + '0' : nibble - 10 + 'A';
nibble = (in[n] & 15); // the LSB nibble
out[2*n+1] = nibble < 10 ? nibble + '0' : nibble - 10 + 'A';
}
if ( 2*n < size_out)
out[2*n] = '\0';
return n;
}
// a little test
int main()
{
unsigned char small[] = { 0x25, 0x37, 0x48, 0x42, 0x42 };
char buffer[41];
size_t size = to_hex( small, sizeof(small), buffer, sizeof(buffer));
cout << "converted bytes " << size << ", hex string " << buffer << "\n";
unsigned char large[256];
for (size_t n=0; n<256; ++n)
{
large[n] = n;
}
size = to_hex( large, sizeof(large), buffer, sizeof(buffer));
cout << "converted bytes " << size << ", hex string " << buffer << "\n";
}
"In testa che avete, Signor di Ceprano?" -- Rigoletto