Support Forum Softcomplex logo
About Us  | Services  | Download  | Order  | Support  | |
| Search | Today's Posts | Stats | Board Rules | Not logged in [ Login | Register ]
go to bottom
Last active: Never

Printable Version | Subscribe | Add to Favourites   Post new thread
Author: Subject: JavaScript to convert Hex to Decimal Numbers
tigra
Administrator

Posts: 2049
Registered: 6/17/2002
Location: US, CO
Member Is Offline
posted on 8/2/2007 at 06:32 PM Reply With Quote
JavaScript to convert Hex to Decimal Numbers


Today I stumbled upon the need to convert hex color codes to their decimal values. I found a lot of samples where people exercised their algorithmic skills, some are good, some not. Here is my JavaScript specific solution:

Code:

function hex2num (s_hex) {
eval("var n_num=0X" + s_hex);
return n_num;
}


In my case validation wasn't needed, but generally it's a good idea. With the input validation the code look like:

Code:

var re_hexNum = /^[\da-f]+$/i;
function hex2num (s_hex) {
if (!re_hexNum.exec(s_hex))
return alert ('error');
eval("var n_num=0X" + s_hex);
return n_num;
}


In some cases '#' has to be removed from the beginning of the hex value. I leave this exercise to the reader.
View User's Profile View All Posts By User U2U Member
abefimrs
Newbie

Posts: 1
Registered: 12/27/2008
Location: Dhaka, Bangladesh
Member Is Offline
posted on 12/27/2008 at 12:05 PM Reply With Quote
Decimal to Hexa deci


Is there any sort way to convert decimal to Hexa..
View User's Profile View All Posts By User U2U Member
tigra
Administrator

Posts: 2049
Registered: 6/17/2002
Location: US, CO
Member Is Offline
posted on 12/28/2008 at 03:14 AM Reply With Quote

I stumbled on two short functions:

function d2h(d) {return d.toString(16);} // decimal to hex
function h2d(h) {return parseInt(h,16);} // hex to decimal
View User's Profile View All Posts By User U2U Member
Post new thread

Related Links:

Go To Top


Powered by XMB 1.9.1 Nexus
Developed By Aventure Media & The XMB Group © 2002-2004