How to Convert Decimal to Hexadecimal Number in JavaScript
How to Convert Decimal to Hexadecimal Number in JavaScript
Introduction
In this tutorial we will create a How to Convert Decimal to Hexadecimal Number in JavaScript. This tutorial purpose is to teach you on how to convert the decimal to hexadecimal in easiest way. This will tackle all the basic function that will convert the number into hexadecimal format. I will provide a sample program to show the actual coding of this tutorial.
This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application that needed to convert any decimal number. I will give my best to provide you the easiest way of creating this program Decimal to Hexadecimal number. So let's do the coding.
Before we get started:
This is the link for the template that i used for the layout design https://getbootstrap.com/.
Creating The Interface
This is where we will create a simple interface for our application. This code will only display the html forms and the result section. To create this simply copy and write it into your text editor, then save it as index.html.- <!DOCTYPE html>
- <html lang="en">
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-4">
- <div class="form-group">
- <input type = "number" id="decimal" class="form-control"/>
- </div>
- </div>
- <div class="col-md-8">
- </div>
- </div>
- </body>
- </html>
Creating JavaScript Function
This is where the main function of the application is. This code will convert any decimal number you enter into hexadecimal dynamically. To do this just copy and write these block of codes inside the text editor and save it as script.js.- function clear(){
- document.getElementById("decimal").value = "";
- document.getElementById("result").innerHTML = "";
- }
- function decimalToHex() {
- let decimal = document.getElementById("decimal").value;
- if(decimal == ""){
- alert("Please fill up the required field!");
- }else{
- decimal = parseInt(decimal);
- let hexadecimal = decimal.toString(16);
- document.getElementById("result").innerHTML = "<center><h1 style='font-size:18px;'>HEXADECIMAL</h1></center><center><h2 class='text-primary'>"+hexadecimal+"</h2></center>";
- }
- }
In the code above we just simply create only one method called decimalToHex(). This function is the one that will convert the decimal number you enter into a hexadecimal format.
In order to convert it into hexadecimal number, we need to convert the decimal first into int type using the parseInt() function. After that we will use the toString() function with the value of 16 to force the decimal value into a hexadecimal format.
Output:
The How to Convert Decimal to Hexadecimal Number in JavaScript source code that I provide can be download below. Please kindly click the download button.
There you have it we successfully created How to Convert Decimal to Hexadecimal Number in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for JavaScript Language
Add new comment
- 140 views