473,890 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting Hex or Binary to Decimal and Decimal to Hex or Binary JavaScript Example

Frinavale
9,735 Recognized Expert Moderator Expert
Convert a Hex number into a decimal number and a decimal number to Hex number

This is a very simple script that converts decimal numbers into hex values and hex values into decimal numbers. The example following this one demonstrates how to convert decimal numbers into binary values.

A hex number is a string that contains numbers between 0-9 and characters A-F; where A = 10, B = 11, ..., F = 15. (For more information please look up (google) Hex numbers.)

With this in mind, it's clear that you need to call the Number Object's toString() method in order to convert a Number into a Hex value. When you call the toString() method simply pass it 16 as a parameter and it will print the number in base 16 (hex).

To convert a hex number into a decimal number you can use the parseInt() method. You pass this method the string containing the hex value as the first parameter and 16 as second parameter to indicate that the string is a base 16 number. This will return you a Number Object that is the decimal (base 10) equivalent to the hex value.

Expand|Select|Wrap|Line Numbers
  1. <html xmlns="http://www.w3.org/1999/xhtml"> 
  2.   <head></head>
  3.   <body>
  4.     <script type="text/javascript">
  5.       function ConvertToHex(){
  6.         var numberValue = document.getElementById('NumberInput').value;
  7.         var decNumber = Number(numberValue);
  8.         var hexNumber = decNumber.toString(16).toUpperCase();
  9.  
  10.         document.getElementById('Result').value = hexNumber;
  11.       }
  12.       function ConvertToDec(){
  13.         var hexNumber = document.getElementById('NumberInput').value;
  14.         var decNumber = parseInt(hexNumber,16);
  15.         document.getElementById('Result').value = decNumber;
  16.       }
  17.     </script>
  18.  
  19.     <div style="text-align:center">
  20.       Number: <input type="text" id="NumberInput"></input>
  21.       Result: <input type="text" id="Result"></input>
  22.       <br/>
  23.       <button onclick="ConvertToHex();">Convert To Hex</button>
  24.       <button onclick="ConvertToDec();">Convert To Decimal</button>
  25.       <br />
  26.     </div>
  27.   </body>
  28. </html>
Convert a Binary number into a decimal number and a decimal number to binary number

This technique can be used to convert numbers into whatever base you want! For example if you want to convert a decimal number into binary you just use base 2 instead of base 16:
Expand|Select|Wrap|Line Numbers
  1. <html xmlns="http://www.w3.org/1999/xhtml"> 
  2.   <head></head>
  3.   <body>
  4.     <script type="text/javascript">
  5.       function ConvertToBinary(){
  6.         var numberValue = document.getElementById('NumberInput').value;
  7.         var decNumber = Number(numberValue);
  8.         var binaryNumber = decNumber.toString(2).toUpperCase();
  9.  
  10.         document.getElementById('Result').value = binaryNumber;
  11.       }
  12.       function ConvertToDec(){
  13.         var binaryNumber = document.getElementById('NumberInput').value;
  14.         var decNumber = parseInt(binaryNumber, 2);
  15.         document.getElementById('Result').value = decNumber;
  16.       }
  17.     </script>
  18.  
  19.     <div style="text-align:center">
  20.       Number: <input type="text" id="NumberInput"></input>
  21.       Result: <input type="text" id="Result"></input>
  22.       <br/>
  23.       <button onclick="ConvertToBinary();">Convert To Binary</button>
  24.       <button onclick="ConvertToDec();">Convert To Decimal</button>
  25.       <br />
  26.     </div>
  27.   </body>
  28. </html>
Happy Coding!


-Frinny
Apr 7 '09 #1
0 28099

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3227
by: Sverre Bakke | last post by:
Hi I am using the base_convert() function to convert numbers between binary, hexadecimal, decimal, etc... It works great, but I have problems converting numbers with .'s Like this number: 15.20 decimal will be 1111 as binary...
2
7685
by: Mariusz Sakowski | last post by:
I'm writing class which will be able to store large numbers (my ambition is to make it able to operand on thousands of bits) and perform various operations on it (similiar to those available with int type). I want to store the number as binary stream, and I don't know how to convert large decimal string (char*) to binary stream. Converting using MOD would last for hours with really big numbers, and in fact I don't know any other ways to...
25
6788
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual bytes. I've tried using bitwise operators, but they seem to convert the value into an integer first, and i've tried using the toString() method to convert it into a hex value so i can parse it, but that also seems to first convert it into an...
2
3643
by: geskerrett | last post by:
In the '80's, Microsoft had a proprietary binary structure to handle floating point numbers, In a previous thread, Bengt Richter posted some example code in how to convert these to python floats; http://groups.google.com/group/comp.lang.python/browse_thread/thread/42150ccc20a1d8d5/4aadc71be8aeddbe#4aadc71be8aeddbe I copied this code and modified it slightly, however, you will notice that for one of the examples, the conversion isn't...
7
17109
by: dlarock | last post by:
I wrote the following to do an MD5 hash. However, I have a problem (I think) with the conversion from the Byte MD5 hash back to string. Watching this through the debugger it appears as if the MD5 is computing the right Byte for the hash when compared to other MD5 hash generators online. However, when I attempt to convert it back to tring using the line String outputData = textConverter.GetString( result ) ; I essentially get...
15
35539
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
5
5591
by: Jeff Dillon | last post by:
How might I convert a string like 10.A (in hex) to it's decimal equivalent? Basically I have an input string like ((1F.A + 3A.D) - 1F.E) and need to calculate the result. Using Reflection and dynamic compilation, I was able to create a VB.NET equivalent of the older Eval function, but it only works with decimal numbers. Any thoughts? I know the logic to convert, but I was hoping for some built-in .NET methods that I could leverage jeff
2
4513
by: Tukeind | last post by:
Hello, I am receiving the following error: error C2065: 'to_binary' : undeclared identifier while running the code below. If anyone can help I'll appreciate it? Thank you, Tukeind
0
3360
by: Terry Reedy | last post by:
A. Joseph wrote: These are number representation systems that can be applied to or used with integral, rational (numberator,denominator), and 'point' numbers. Try Wikipedia or any search engine. There are standard algorithms for converting between representations. See above. Good programmer calculators have these built in.
0
9976
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9815
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9618
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7160
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4665
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4259
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.