Converting RGB Integers to uint32_t 0xFFFFFF

The title pretty much sums up all that I'm trying to accomplish. I am currently trying to to take 3 incoming RGB values, say the color orange, where R = 255, G = 165, B = 0, to a uint32_t HEX color code, ie. 0xFFA500. I understand the first two bytes correspond to red, the second green, so forth. I do not now what code to right to convert the 3 integers to bytes, then string all three and the leading 0x together.

All help is appreciated. Thanks.

Have you written any code or made an attempt to write any code?

One way is to use a UNION.

union COLOUR
{
unsigned long RGB;
byte bytes[3];
};

No I have not written any code yet. I have tried but can not materialize my thoughts, as this is uncharted territory for me. I have read about how you can divide by 16 and then multiple the remainder ma thematic solution, still don't know how to combine the bytes. The union idea is way over my head, larryd, do you have another way, or explain how that code you provided works? would be sooo appreciated. Best.

bytes[0], bytes[1] and bytes[2] are at the same address as RGB


//where R = 255, G = 165, B = 0

union COLOUR
{
unsigned long RGB;
byte bytes[3];
};

union COLOUR assembledColour;
. . .
assembledColour.RGB = 0; //clean out the memory

assembledColour.bytes[0] = 0; //blue = 00
assembledColour.bytes[1] = 165; //green = A5
assembledColour.bytes[2] = 255; //red = FF

Serial.println(assembledColour.RGB, HEX); //this will print FFA500

1 Like

Thank you larryd. How can I save the output of that serial.Println to a variable? And concatenate 0x before it?

unsigned long myVariable;
. . .
Serial.print(‚Äú0x‚ÄĚ); //prints 0x
Serial.println(assembledColour.RGB, HEX); //this will print FFA500 therefore you get 0xFFA500

myVariable = assembledColour.RGB; //saves the assembled RGB value in variable myVariable

Sketch

//where R = 255, G = 165, B = 0

unsigned long myVariable;

union COLOUR
{
  unsigned long RGB;
  byte bytes[3];                              //bytes[0] is LSB, bytes[2] is MSB
};

union COLOUR assembledColour;

//**************************************************************************
void setup()
{
  Serial.begin(9600);

  assembledColour.RGB = 0;                   //clean out the memory

  assembledColour.bytes[0] = 0;              //blue  = 00   LSB
  assembledColour.bytes[1] = 165;            //green = A5
  assembledColour.bytes[2] = 255;            //red   = FF   MSB

  Serial.print("0x");                        //prints 0x
  Serial.println(assembledColour.RGB, HEX);  //this will print FFA500 therefore you get 0xFFA500

  myVariable = assembledColour.RGB;          //saves the assembled RGB value in variable myVariable

}

//**************************************************************************
void loop()
{

}

//**************************************************************************

That code works! You are one of the few respondents I have come across that doesn't beat around the bush and answers the question succinctly. Thank you much.