Hex to decimal conversion possible?


#1

I need to convert an RGB hex string to three dec integer values. I can split the string easily enough but there doesn’t seem to be a function to change hex to decimal and I can’t just use my hex strings to assign int variables. Whether it’s preceded 0x or # it comes back as 0. Am I missing a thing, or is it simply not considered something I’d want to do?

Oddly, a search returns no results at all - it’s like I’m the only person who ever wanted to do this.

Thanks in advance
Paul


#2

Just off the top of my head (and a quick Google search), you could program the math…


#3

I notice there is an ‘hslToHex()’ function which can create your HEX value from Hue, Saturation, and Level but nothing to go the other way. You would need three functions to go the other way since you need to return 3 values. As @Pantheon stated, you can do the math. Since they are all just two characters, should be straightforward. You will need to get the decimal value for each character. I suggest something like this:

hexIndex='0123456789abcdef'

decChar1=indexOf(hexIndex,hexChar1)
decChar2=indexOf(hexIndex,hexChar2)

decValue=decChar1*16+decChar2

#4

That was much easier than I thought! Add @guxdude’s code above and BAM!


#5

you could also use:

decChar1_1=left(HEX1,1)
decChar1_2=right(HEX1,1)
decChar2_1=left(HEX2,1)
decChar2_2=right(HEX2,1)

mid will definitely work better if you don’t want to split the 6 characters up into individual strings:

hexString="0xa7b933"
decChar1_1=mid(hexString,2,1)
decChar1_2=mid(hexString,3,1)
decChar2_1=mid(hexString,4,1)
decChar2_2=mid(hexString,5,1)
decChar3_1=mid(hexString,6,1)
decChar3_2=mid(hexString,7,1)

#6

Sad thing is…we probably just re-invented the wheel. I bet someone out there already has this piston!


#7

I’m sure you are right. Just need one person to post it so others can find it. :laughing:


#8

Can never have enough wheels lol, oh and as long as you don’t have to carry them home.


#9

This is perfect thanks. I understand the math but wasn’t thinking far enough outside the box to use indexOf


#10

In case it helps anyone else, here’s the completed piston for this. I wanted to use a colour control switch to change the lights behind the TV. They are connected using Hyperion and only accept an array of decimal values in a raw json-rpc post