Maverickgugu
Maverickgugu

Reputation: 777

How to convert an Unsigned Character array into a hexadecimal string in C

Is it possible to represent an unsigned character array as a string?

When I searched for it, I found out that only memset() was able to do this (But character by character). Assuming that is not the correct way, is there a way to do the conversion?

Context: I am trying to store the output of a cryptographic hash function which happens to be an array of unsigned characters.
eg:

unsigned char data[N]; ...
for(i=0;i<N;i++) printf("%x",data[i]);

My goal is to represent the data as a String (%s) rather than access it by each element. Since I need the output of the hash as a String for further processing.

Thanks!

Upvotes: 7

Views: 30623

Answers (5)

Roman Slyepko
Roman Slyepko

Reputation: 1393

I successfully use this to convert unsigned char array to std:string

unsigned char array[128];
std::stringstream buffer;
for (int i = 0; i < 128; i++)
    {
        buffer << std::hex << std::setfill('0');
        buffer << std::setw(2)  << static_cast<unsigned>(array[i]);
    }
std::string hexString = buffer.str();

Upvotes: 1

forsvarir
forsvarir

Reputation: 10839

So, based on your update, are you talking about trying to convert a unsigned char buffer into a hexadecimal interpretation, something like this:

#define bufferSize 10
int main() {
  unsigned char buffer[bufferSize]={1,2,3,4,5,6,7,8,9,10};
  char converted[bufferSize*2 + 1];
  int i;

  for(i=0;i<bufferSize;i++) {
    sprintf(&converted[i*2], "%02X", buffer[i]);

    /* equivalent using snprintf, notice len field keeps reducing
       with each pass, to prevent overruns

    snprintf(&converted[i*2], sizeof(converted)-(i*2),"%02X", buffer[i]);
    */

  }
  printf("%s\n", converted);

  return 0;
}

Which outputs:

0102030405060708090A

Upvotes: 22

Batman
Batman

Reputation: 1324

An example as you've asked:

unsigned char arr [SIZE];

Upvotes: -1

Chris
Chris

Reputation: 2060

Well a string in C is nothing else than a few chars one after another. If they are unsigned or signed is not much of a problem, you can easily cast them.

So to get a string out of a unsigned char array all you have to do is to make sure that the last byte is a terminating byte '\0' and then cast this array to char * (or copy it into a array of char)

Upvotes: 2

unwind
unwind

Reputation: 399813

In C, a string is an array of char, terminated with a character whose value is 0.

Whether or not char is a signed or unsigned type is not specified by the language, you have to be explicit and use unsigned char or signed char if you really care.

It's not clear what you mean by "representing" an unsigned character array as string. It's easy enough to cast away the sign, if you want to do something like:

const unsigned char abc[] = { 65, 66,67, 0 }; // ASCII values for 'A', 'B', 'C'.

printf("The English alphabet starts out with '%s'\n", (const char *) abc);

This will work, to printf() there isn't much difference, it will see a pointer to an array of characters and interpret them as a string.

Of course, if you're on a system that doesn't use ASCII, there might creep in cases where doing this won't work. Again, your question isn't very clear.

Upvotes: 2

Related Questions