Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert decimal to hex 1

Status
Not open for further replies.

snotmare

Programmer
Jan 15, 2004
262
US
Greetings!

Perhaps I'm just not looking hard enough, but I can't find any simple method to convert a decimal number to hex.

So, if I input "16777215", I want to return "FFFFFF".

Can someone show me what I'm missing here? Thanks!


---------------------
He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
You could use:
[tt]
hex_string = dec_number.toString(16);
[/tt]
 
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]

<html>
<head>
<title>Untitled</title>

<script type="text/javascript"><!--
var hD="0123456789ABCDEF";
function d2h(d) {
var h = hD.substr(d&15,1);
while(d>15) {d>>=4;h=hD.substr(d&15,1)+h;}
document.getElementById("myTxt2").value = h;
}
function h2d(h) {return parseInt(h,16);} </script>
</head>


<body>
<table border="1">
<input type="text" value="" id="myTxt">
<input type="text" value="" id="myTxt2">
<button onclick="d2h(document.getElementById('myTxt').value)">convert</button>

</body>
</html>

Function was taken from


I don't know the answer but my good friend Google does.
 
Excellent TonyGroves, I knew there had to be something like that out there!


---------------------
He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top