# Javascript: Decimal to Hex Problem

Hello all,

i would like to convert Decimal to Hex.

the problem i met is:

wenn i tried to convert decimal 987654321 to Hex, i get the value 7267464261. What i really wanted are 3AD168B1.

following are codes.

anyone knows where is the problem?

thanks,
wantime
``````<html>
<body>

<p id="demo"></p>

<script type="text/javascript">
function DecToHex(d) {return d.toString(8)}
var dx = DecToHex(987654321);
document.getElementById("demo").innerHTML= dx;
}

</script>

</body>
</html>
``````
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:

//you are getting Octal, change the base to 16

function DecToHex(d) {return d.toString(16)}
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:

``````yourNum = yourNum.toString(16);
and reverse the process with:
yourNum = parseInt(yourNum, 16);
``````

reference: http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript

Hex to Decimal and Decimal to Hex

``````function d2h(d) {return d.toString(16);}
function h2d(h) {return parseInt(h,16);}
``````

In your case just copy past below code and replace your old one it will work fine:

``````<html>
<body>

<p id="demo"></p>

<script type="text/javascript">
function DecToHex(d) {return d.toString(16);}
var dx = DecToHex(987654321);
document.getElementById("demo").innerHTML= dx;
}

</script>

</body>
</html>
``````

Thanks,

Asif Ahmed Khan
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.