How to convert decimal to hex in JavaScript ? - GeeksforGeeks

11 Jul, 2019
Given a number and the task is to convert the number from decimal to hex. This can be done by using toString() method. It takes the parameter which is the base of the converted string. In this case the base will be 16.
Syntax:
decimalNumber.toString( radix )
Parameters: It accepts two parameters which are listed below:
decimalNumber: It holds the number in decimal format which need to be converted.
base: It holds the base of number system in which number is converted.
Example 1: This example converts 20 to hex number system.
<!DOCTYPE html> <html>   <head>     <title>         How to convert decimal to        hex in JavaScript ?    </title></head>           <body style = "text-align:center;">           <h1 style = "color:green;" >         GeeksForGeeks     </h1>                   <p id="up"></p>          <button onclick="myGFG()">         Convert to hex    </button>           <p id="down" style="color: green"></p>                  <script>         var GFG_Var = 20;        var up = document.getElementById("up");        up.innerHTML = GFG_Var;        var down = document.getElementById("down");              function myGFG() {            var GFG_Var2 = GFG_Var.toString(16);             down = document.getElementById("down");            down.innerHTML = "hex of "+ GFG_Var                    + " is = " + GFG_Var2;        }    </script> </body>   </html>                                
Output:
Before clicking the button:
After clicking the button:
Example 2: This example converts a number 100 to its octal number system.
<!DOCTYPE html> <html>     <head>         <title>             Decimal to octal number             system in JavaScript        </title>    </head>               <body style = "text-align:center;">               <h1 style = "color:green;" >             GeeksForGeeks         </h1>                       <p id = "up"></p>                  <button onclick="myGFG()">             Convert to octal        </button>                   <p id = "down" style="color: green"></p>                      <script>             var GFG_Var = 100;            var up = document.getElementById("up");            up.innerHTML = GFG_Var;            var down = document.getElementById("down");                          function myGFG() {                var GFG_Var2 = GFG_Var.toString(8);                 down = document.getElementById("down");                down.innerHTML = "octal of "+ GFG_Var                         + " is = " + GFG_Var2;            }        </script>     </body> </html>                            
Output:
Before clicking on the button:
After clicking on the button:
JavaScript-Misc
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?