I have a function that takes 24 bit to 12 bit hex and prints the colour value on an LCD.
If a user inputs the RGB values, how can I convert the RGB to a single 24-bit hex string?
I found many Java and C++ algorithms online, but I can’t seem to make them compatible with the Arduino.
Last I looked, Arduino doesn’t have a nice way to print other formats, like with printf(). So you’ll have to convert to hex yourself or find an existing function that does it by turning real ints to hex character representations. Here’s one way:
I assume that “24 bit” is the RGB values and that’s 3 8 bit numbers of 0-255, as per usual. In that case, you’ll have two digits of hex for each decimal number. Let’s illustrate with R = 243.
The first hex “digit” will be
h1 = int(R/16)which is 15.The other will be
h2 = R - h1*16which is 3.But those “digits” are still decimal numbers. So have a lookup table in an array, for instance, where the index of the array is the decimal number and the value is the hex character.
So for instance you’ll have
lookup[15] = 'F'andlookup[3] = '3'.Just print out all these digits with no whitespace and you are done.