Converting values to HEX

Simple enough subject ...

I need to take an INT value to HEX, however it needs to be in the 0xnn form. For example, 255 => 0xFF, not just FF. INT 156 to 0x9C.

That value then gets passed to a LED library:

long color = 0;
color <<= 8;
color |= 0xFF; // red
color <<= 8;
color |= 0x9C; // green
color <<= 8;
color |= 0x00; // blue

And then 'color' gets pushed out. So I want to take those INTs that I'm generating, and be able to pass them to the library as valid HEX values.

So I can convert the INT to a HEX already, but that just gives me the last part of what I need, without the 0x ... so how do I do this properly?

Thanks.

The 0x prefix is a clue to the compiler that the numbers/characters that follow are a number in base 16, not base 10. Internally, the value is stored in a variable in binary.

So I can convert the INT to a HEX already, but that just gives me the last part of what I need, without the 0x ... so how do I do this properly?

Where is this int coming from? Are you parsing a string? Converting an int to hex really does not make much sense, unless you are talking about converting an int to a printable format using hex notation.

The numbers are all handled as binary inside the uC, showing them in DEC format (base 10, such as 255) or HEX (0xFF) is just a nicety for us humans.
I think if you try a test with shifts and compound ORs you will get the correct results back.

Where does " 'color' gets pushed out " go to? Serial monitor?

Serial.print ("0x"0);
Serial.println (color, HEX);

should print out 0x00FF9C00,
but the library will see it as 00000000111111111001110000000000

PaulS:
Where is this int coming from? Are you parsing a string? Converting an int to hex really does not make much sense, unless you are talking about converting an int to a printable format using hex notation.

That int is coming from an HSL2RGB routine that spits out integers between 0 and 255 for each color. So I'll get something like:
r = 35
g = 123
b = 0
for example.

I need to take those and convert them to hex and then feed them to the code that's posted on Sparkfun's website.

It's that code that wants something between 0x00 and 0xFF to feed into the 'color' value.

I think you can do like you showed tho:

long color = 0; // clear it out
color = r;
color <<8;
color = color | g;
color <<8;
color = color | b;

now color = 0x0rgb; // with rgb replaced by their hex values of decimal values above

0x00237B00 and 0x00FFFFFF at the highest

should print out 0x00FF9C00,

The Serial library provides no provision for printing leading zeroes. It would only print 0xFF9c00 (which may still work just fine anyways).

sprintf (all version of printf actually), does provide an option to include leading zeroes. For a 4 byte value, something like this would work:

sprintf(buf, "0x%08X", color);

That will put the string "0x00FF9c00" in the char array buf (which needs to be large enough to contain the entire string)

That's fine though, I don't need to print anything to the Serial monitor. I will try what CrossRoads suggested when I get home later this evening. Thanks guys.

CrossRoads:
I think you can do like you showed tho:

long color = 0; // clear it out
color = r;
color <<8;
color = color | g;
color <<8;
color = color | b;

now color = 0x0rgb; // with rgb replaced by their hex values of decimal values above

0x00237B00 and 0x00FFFFFF at the highest

That worked with one small change:

color << 8;

should be:

color <<= 8;

Works like a charm though. Thanks!

That's what you started with tho, just with variable name in place of a hard value.
Just took some convincing XD