0

I have a bitmap stocked as a unsigned char array, containing only 1 and 0; like this:

0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1

I wish to stock this in a compact way, so I wrote a function to convert this into hexadecimals. I'll stock this like:

0f33

My question now: with which function can I convert these characters back into my bitmap? when I have a pointer to the character "f", how can I convert that into the integer value 15? (I know a switch case would do the trick, but probably there is a better way?)

Chris Maes
  • 35,025
  • 12
  • 111
  • 136

1 Answers1

1

Try this for C++:

int number;
std::stringsteam ss;
ss << std::hex << *characterPointer;
ss >> number;

For C:

char hexstr[2] = {*characterPointer, '\0'};
int number = (int)strtol(hexstr, NULL, 16);
PlasmaPower
  • 1,864
  • 15
  • 18
  • strtoll will read the whole string into one long number. I just want it to read one char at a time and convert it to the corresponding int... – Chris Maes Mar 07 '14 at 15:15
  • my matrix is quite big so I'll get overflow if i try to read it all at once... and I'll still need to convert back to my char array... – Chris Maes Mar 07 '14 at 15:16
  • @ChrisMaes Yes, you see the first line? It turns the single character into character string with only it and `\0`. – PlasmaPower Mar 07 '14 at 15:17
  • ok I'm sorry, missed that... works fine allright.. was only hoping there would be a better/ more efficient way to do this conversion back and forth.... – Chris Maes Mar 07 '14 at 15:20
  • Why not decoding whole bytes? `char hexstr[3] = {*(c+1), *c, 0 };` – Wolf Mar 07 '14 at 15:25