Page 1 of 1

[SOLVED] Hex values from a string

Posted: Thu Feb 03, 2005 12:15 pm
by Chris Corbyn
I have this string

#f700e2

or something similar (a hex color code)

How do I use javascript to get the red part (f7), the green part (00) and the blue part (e2) as hexadecimal values?

I tried

Code: Select all

var redhex = 0xhexstring.substr(1,2) //Thought it would give 0xf7
But it didn't work... Otherwise how can I convert the string "f7" obtained using substr() into it's decimal value?

Cheers

Posted: Thu Feb 03, 2005 12:41 pm
by feyd
substring() is a string based method in Javascript.

Code: Select all

var hex = '7F2F0F';
var red = hex.substring(0,2); // or something like that..

Posted: Thu Feb 03, 2005 12:59 pm
by Chris Corbyn
Yeah I new that so what I did was I used substr to get the hex pair then parseint(hexvalue,16) to make the hex in the end.

Thanks :-)