JavaScript - Simple Decimal to Hexadecimal Converter

Language

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 https://getbootstrap.com/.

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.

`<!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">			<a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>		</div>	</nav>	<div class="col-md-3"></div>	<div class="col-md-6 well">		<h3 class="text-primary">JavaScript - Simple Decimal to Hexadecimal Converter</h3>		<hr style="border-top:1px dotted #ccc;"/>		<div class="col-md-3"></div>		<div class="col-md-6">			<div class="form-group">				<label>Enter a decimal</label>				<input type = "number" id="decimal" class="form-control"/>			</div>			<center><button class="btn btn-primary" onclick="decimalToHex();">Convert</button> <button class="btn btn-success" onclick="reset();">Reset</button></center> 			<br><br>			<div id = "result"></div>		</div>	</div> <script src="js/script.js"></script></body></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.

`function decimalToHex() {	var decimal = document.getElementById("decimal").value; 	if(decimal == ""){		alert("Please fill up the required field!");	}else{		decimal = parseInt(decimal);		var hexadecimal = decimal.toString(16);		document.getElementById("result").innerHTML = "<center><label style='font-size:18px;'>The hexadecimal number is</label></center><center><h3 class='text-primary'>"+hexadecimal+"</h3></center>";	}} function reset(){	document.getElementById("decimal").value = "";	document.getElementById("result").innerHTML = "";}`

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!