Decimal number to Hex value conversion

We can convert decimal to hexadecimal values in JavaScript by using toString() function. Here is the syntax


If radix is kept at 16 then the output will be in hexadecimal value. Here is an sample

var n1=12;
document.write (n1.toString(16));

The output of above string is c ( Hex value of 12 )
The input variable must be an integer. So we will use paraseInt() function to convert any input to pure integer value.

We will also use isNan function to check the user input.

Demo of converting any decimal value to Hex

Here is the script of this demo

<title>Demo of decimal to hexadecimal conversion</title>
<script language='JavaScript' type='text/JavaScript'>
function hex_convt(){
var n1=document.getElementById('t1').value;
document.getElementById("d1").innerHTML=document.getElementById('t1').value + " is not a number ";
// Change the toString(16) to toString(8) or toString(2) get Octal and binary conversion



<input type=text name=t1 id='t1' size=8>
<input type=button name='SB' value='Change to Hex' onclick="hex_convt()";>
<div id=d1></div>

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here .

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer