javascript
Parameters: hexString (String)
A hexadecimal number in string format
Returns: Decimal equivalent of the hexadecimal input
This Javascript function takes a hexadecimal number as input and outputs its decimal equivalent. It uses the built-in parseInt method.
Hello there programmer! It's awesome that you're here. In this post, we will bravely voyage into the realm of JavaScript to program a function named 'convertHexadecimalToDecimal'. This function, as you might guess, will take a hexadecimal number and convert it to its decimal equivalent. If you're up for a simple but rewarding coding challenge, then this is the place to be. Let's get to it!
Before we begin coding, here's some essential background information. A hexadecimal number is a number system with base 16. It includes the digits 0-9 and the letters A-F where A is equivalent to decimal 10, B is 11, till F which is 15. We want to convert this into a decimal, a number system with base 10 (0-9).
We'll start with creating a function convertHexadecimalToDecimal
accepting a single parameter that represents the hexadecimal number.
function convertHexadecimalToDecimal(hex) {
let decimal = 0;
}
Next, we will iterate over the hexadecimal string in reverse. The reasoning behind this is that the least significant digit (the rightmost one) has '2^0' weightage, the second least significant digit has '2^1', and so on until the most significant digit (the leftmost one).
function convertHexadecimalToDecimal(hex) {
let decimal = 0;
for(let i = hex.length - 1; i >= 0; i--) {
// To be completed next...
}
}
Up until now, we've just skimmed through the hex number in reverse. Now, at every step of the iteration, we will convert the ith digit to a decimal number and add it to our total (named 'decimal'). For digits that are alphabets, we will subtract 55 from their ASCII equivalent (as ASCII value of 'A' is 65 and 'A' represents 10 in hexadecimal, so 65-55 equals 10, and so on).
function convertHexadecimalToDecimal(hex) {
let decimal = 0;
for(let i = hex.length - 1; i >= 0; i--) {
let digit = Number(hex.charAt(i));
if(isNaN(digit)) {
digit = hex.charCodeAt(i) - 55;
}
decimal += digit * Math.pow(16, hex.length - 1 - i);
}
return decimal;
}
That's it! We've completed our function.
So here we have our function to convert a hexadecimal number to a decimal one. Remember that the input for the function is a string representing a hexadecimal number, and the output is a number representing the corresponding decimal number. If the input string has a character that is not a valid hexadecimal character, our code will not handle it properly, so make sure you input valid hexadecimal numbers only! Our function uses the built-in JavaScript functions isNaN
, charCodeAt
and Math.pow
to handle the necessary conversions and calculations.
In mathematics, Hexadecimal is a base 16 system used to simplify how binary is represented. A hexadecimal digit can be any of the following 16 digits: `0 1 2 3 4 5 6 7 8 9 A B C D E F`. To convert hexadecimal to decimal, start from the right-most digit (also known as the least significant digit) and multiply it with 16 raised to the power of the position index (starting from 0) and continue doing so till you reach the left-most digit (most significant digit).
Learn more