JavaScript - Simple Decimal to Hexadecimal Converter

Submitted by: 
Visitors have accessed this post 103 times.

In this tutorial we will create a Simple Decimal to Hexadecimal Converter using JavaScript. This code will convert the given decimal into hexadecimal when the user click the convert button. The code use onclick() function to parse the given value as int in order to convert the value into hexadecimal by adding a dot notation string(16). Feel free to modify and apply it in your system, this is a user-friendly kind of program

We will be using JavaScript as a server-side scripting language because It gives a greater control of your web page and extend its capability in a modern way approach. It is written in HTML or as an external sourcing to add some necessary features in your website.

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5. <nav class="navbar navbar-default">
  6. <div class="container-fluid">
  7. <a class="navbar-brand" href="<a href="">Sourcecodester</a>
  8. " rel="nofollow">">Sourcecodester</a>
  9. </a> </div>
  10. </nav>
  11. <div class="col-md-3"></div>
  12. <div class="col-md-6 well">
  13. <h3 class="text-primary">JavaScript - Simple Decimal to Hexadecimal Converter</h3>
  14. <hr style="border-top:1px dotted #ccc;"/>
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6">
  17. <div class="form-group">
  18. <label>Enter a decimal</label>
  19. <input type = "number" id="decimal" class="form-control"/>
  20. </div>
  21. <center><button class="btn btn-primary" onclick="decimalToHex();">Convert</button> <button class="btn btn-success" onclick="reset();">Reset</button></center>
  23. <br><br>
  24. <div id = "result"></div>
  25. </div>
  26. </div>
  28. <script src="js/script.js"></script>
  29. </body>
  30. </html>

Creating the Script

This code contains the script of the application. This code will convert the decimal into hexadecimal when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.

  1. function decimalToHex() {
  2. var decimal = document.getElementById("decimal").value;
  4. if(decimal == ""){
  5. alert("Please fill up the required field!");
  6. }else{
  7. decimal = parseInt(decimal);
  8. var hexadecimal = decimal.toString(16);
  9. document.getElementById("result").innerHTML = "<center><label style='font-size:18px;'>The hexadecimal number is</label></center><center><h3 class='text-primary'>"+hexadecimal+"</h3></center>";
  10. }
  11. }
  13. function reset(){
  14. document.getElementById("decimal").value = "";
  15. document.getElementById("result").innerHTML = "";
  16. }

There you have it we successfully created a Simple Decimal to Hexadecimal Converter using 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!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.