Fill in a passed char array

I’m trying to convert a color code from a 3 byte array to HTML notation (i.e. “#DA80FF”) without using Strings.
My approach is declaring this:

byte myByteArray[] = {0x20, 0xFF, 0x80};
char myCharArray[8];

…and performing a function call like this whenever needed:

byteToHTML(myCharArray, myByteArray);

Here’s the function code:

void byteToHTML(char* dest, byte* source) {
  dest[0] = '#';
  byte b;
  for (byte i = 0; i < 3; i++) {
    b = ((byte)(source[i] >> 4));
    dest[i*2+1] = (char)(b > 9 ? b + 0x37 : b + 0x30);
    b = ((byte)(source[i] & 0xF));
    dest[i*2+2] = (char)(b > 9 ? b + 0x37 : b + 0x30);
  }
  dest[7] = NULL;
}

I tried referencing and derefererencing arrays here and there, but I got lost in the process.

    b = ((byte)(source[i] >> 4));

What type is source[ i ]? Why is it necessary to cast a byte to a byte?

The code you posted does something. You didn't say what it does. You expect it to do something. You did not say what.

Look up the sprintf function, then you can replace your whole function with one line of code.

PaulS: What type is source[ i ]? Why is it necessary to cast a byte to a byte?

Ha, I messed aroud so much with this bunch of lines of code that I forgot a now useless cast.

PaulS: The code you posted does something. You didn't say what it does. You expect it to do something. You did not say what.

Well, I wanted to slice the first and the last nibble of the byte. It seemed legit to me to perform a bitshift to get the most significant nibble and an "& 1111" for the least significant one.

Anyway, I'm not using sprintf() since I can't simply fire the result on the Serial output.

It seemed legit to me to perform a bitshift to get the most significant nibble and an "& 1111" for the least significant one.

It is. So, what is the problem? Overall, the code does something. You still haven't explained what it does or what your problem is.

I'm not using sprintf() since I can't simply fire the result on the Serial output

sprintf() formats data into a character string, it does not send data to a serial line. http://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm

jremington: sprintf() formats data into a character string, it does not send data to a serial line.

Mmm, I badly needed some rest yesterday... I kept looking at printf() specs the whole afternoon without noticing the missing 's'. Thanks to your advice the code becomes:

sprintf(myCharArray, "%X%X%X", myByteArray[0], myByteArray[1], myByteArray[2]);

I'm loosing 2k of flash memory in the process, but the approach looks less clumsy and I've got plenty of program space at the moment. Anyway, what was going on in my code? It wasn't working at all in the way I meant. I guess I misused the reference operator, so trying to access single chars of a reference of a char array was somewhat wrong. I thought a dereferencing would solve the case, but it didn't.

You may have noticed I'm just a hobbyist...