7

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!

forsvarir
  • 10,749
  • 6
  • 46
  • 77
Maverickgugu
  • 767
  • 4
  • 13
  • 26
  • What is it you're actually trying to achieve? Superficially casting the array to (char*) could achieve what you've asked for – forsvarir Apr 14 '11 at 09:31
  • Isn't an unsigned char array a string anyway? I mean I'm not aware of any negative ASCII values ... – das_weezul Apr 14 '11 at 09:32
  • As an aside, memset sets a buffer to a supplied value, a given number of times, so I can't really see how that would be helping you very much... code/pseudo code sample might help. – forsvarir Apr 14 '11 at 09:35
  • I have edited the question with a context. Please feel free to point out if I am not clear still. – Maverickgugu Apr 14 '11 at 09:57

5 Answers5

22

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
forsvarir
  • 10,749
  • 6
  • 46
  • 77
  • 2
    @Maverickgugu: It is... but it's also dangerous :) Watch out for buffer overruns, since it doesn't do any length checking. There's a safer version (snprintf), which allows you to specify the maximum size that can be written to the buffer which you might want to take a look at. – forsvarir Apr 14 '11 at 13:59
  • @forsvarir: in the same sense.. Can we represent the unsigned character array in the binary representation of the string? Suggestions would be helpful.. – Maverickgugu Apr 16 '11 at 14:00
  • @Maverickgugu: so instead of converting it to a string of: 0A0B etc, you want 000010, 000011 etc? – forsvarir Apr 16 '11 at 15:27
  • @forsvarir: Yes.. Exactly.. Not particularly a string. Even an integer array of 0s and 1s would do.. Will it be possible? I'm out of ideas. – Maverickgugu Apr 16 '11 at 17:20
  • 1
    @Maverickgugu: Haven't you asked this question and had is answered here: http://stackoverflow.com/questions/5680367/wierd-error-abort-trap-while-handling-character-array-in-c – forsvarir Apr 18 '11 at 05:11
2

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.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • I'm working on a MAC, and does type casting still give a junk value if one of the unsigned character has a super dooper value rather than a regular ASCII value? – Maverickgugu Apr 14 '11 at 10:02
2

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)

Chris
  • 2,030
  • 1
  • 16
  • 22
  • Thanks. Solution looks so simple. But do I concat an unsigned character array with a string? Should I insert a null character as the last element? – Maverickgugu Apr 14 '11 at 09:59
  • 1
    Well, in a cryptographic context this might not be so easy, it all depends on how you will process the string. You have to keep in mind that the output of a hashing function may already contain '\0'-bytes, and you might end up just working on a part of the string since string-process functions will stop working if the see such a byte – Chris Apr 14 '11 at 10:02
  • Yes. I completely agree with your reasoning! – Maverickgugu Apr 14 '11 at 10:06
1

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();
Roman Slyepko
  • 1,393
  • 2
  • 8
  • 15
-1

An example as you've asked:

unsigned char arr [SIZE];

Batman
  • 1,244
  • 2
  • 14
  • 26