# RGB to HEX

For a certain application, i need to convert R G B (0-255) values to HEX (like 000000 to FFFFFF). I tried, but i can't get any further than this:

int HEX = String(decimalRGB, HEX);
int HEX2 = String(decimalRGB2, HEX);
int HEX3 = String(decimalRGB3, HEX);

But now i just need to 'Combine' them :S

So you want to convert three numbers to one number?

The best way is to use shifts:

byte R, G, B;
long RGB = ((long)R << 16L) | ((long)G << 8L) | (long)B;

If you then want to print the number as hexadecimal:

Serial.println(RGB, HEX);

Remember that decimal, hexadecimal, octal, binary, characters, etc are just ways of displaying the number. Internally, everything is stored as binary.

EDIT: Made a mistake with type/overflow as pointed out on page 2.

I understand, thanks. Now how do i convert it to an integer?

A long is an integer data type.

WizenedEE:
So you want to convert three numbers to one number?

The best way is to use shifts:

byte R, G, B;

long RGB = (R << 16) | (G << 8) | B;

If you then want to print the number as hexadecimal:

Serial.println(RGB, HEX);

Remember that decimal, hexadecimal, octal, binary, characters, etc are just ways of displaying the number. Internally, everything is stored as binary.

I tried everything, but how do i convert the 0-255 dec to a byte?

With the assignment operator, =

I already tried it like this:

int R12 = 255;
R = (R12, BYTE);

But it won't work...

Why use an int variable when you want a byte variable? I don't understand the second line.

AWOL: Why use an int variable when you want a byte variable?

Because this is just some test code, in the eventual code, another part generates an int from 0-255.

I don't understand the second line.

Me neither, but i couldn't find any information on how to do it else.

I really think you're overthinking this problem, What is "R" in the code above?

Oh yeah, i forgot. Above it is

byte R, G, B;

I'm thinking this could be a lot simpler, but i don't really know how. I have 1 decimal 0-255 integer. I need to convert it to HEX, and repeat it 3 times in one string that's 6 characters long, so even if the original integer is 0, it eventually needs to be 000000. And if the original integer is 10, it needs to be 0A0A0A.

Trying some stuff. Now i got this:

byte R, G, B;
int R12 = 255;
int G12 = 0;
int B12 = 255;
R = R12;
G = G12;
B = B12;
long RGB = (R << 16) | (G << 8) | B;
Serial.println(RGB, HEX);

But it just returns FF in staead of FF00FF

It doesn't matter whether your code uses Hex, Dec, Binary, as the compiler converts it all the binary anyway (the language the microcontroller speaks).

I gather what you are trying to do is to send a number to the Serial port as a series of characters which represent the hexadecimal form of the number. Something like this:

byte R,G,B;

R = something.
G = something.
B = something.

char hex[7] = {0};
sprintf(hex,"%02X%02X%02X",R,G,B); //convert to an hexadecimal string. Lookup sprintf for what %02X means.
Serial.println(hex); //Print the string.

Wow, thank you Tom. You just made my life so much easier!

Now i'm trying to use the 'hex' in a function which needs an integer, and it gives me the following error: invalid conversion from 'char*' to 'int'.

I really think you need to explain what you are trying to do. Tom gave you a way to output, to serial, a hex representation of your three bytes, by generating a string, because that is what you appeared to want. If you need to do anything else with it, then the long variable in an earlier post will be needed.

long RGB = (R << 16) | (G << 8) | B;

The reason your code only showed ‘FF’ instead of the expected ‘FF00FF’:

byte << int results in an 16-bit int. If you shift the byte left by 16 (as in R) it gets shifted off the end.

Use byte << 16L to get a long int (32-bit) result which will hold the 24-bit result:

long RGB = (R << 16L) | (G << 8) | B;

You were getting 0x0000 | 0xGG00 | 0x00BB = 0xGGBB which prints as ‘FF’.

What you wanted was 0x00RR0000 | 0xGG00 | 0x00BB = 0x00RRGGZBB which prints as ‘FF00FF’.

You can’t pass a 24-bit value to a functions that use an int. SOMETHING is way wrong. What function are you trying to pass RGB to?

The 'Rectangle' function in a uOLED library, this is what the manual says:

void uOLED::Rectangle   (   char    x1,
char    y1,
char    x2,
char    y2,
int     color,
char    filled
)
Draw a rectangle.

Draw a rectangle with upper-left corner at x1,y1 and the bottom-right corner at x2,y2. Lines will have 16bit color. filled=true -> fill the rectangle with the color given.
Note that the sides of the rectangle can not be diagonal on the screen.

What i'm trying to generate, is the HEX color int.

Where does it mention hex? The color parameter is only 16 bits wide, you can't pass three bytes to it. You need to know how colour is represented in that library.

If you'd mentioned this at the beginning, a lot of time could have been saved.