Map an integer from decimal base to hexadecimal with custom mapping JavaScript

Usually when we convert a decimal to hexadecimal (base 16) we use the set 0123456789ABCDEF to map the number.

We are required to write a function that does exactly the same but provides user the freedom to use any scale rather than the one mentioned above.

For example ?

The hexadecimal notation of the decimal 363 is 16B
But if the user decides to use, say, a scale 'qwertyuiopasdfgh' instead of
'0123456789ABCDEF', the number 363, then will be represented by wus

That's what we are required to do.

So, let's do this by making a function toHex() that makes use of recursion to build a hex out of the integer. To be precise, it will take in four arguments, but out of those four, only the first two will be of use for the end user.

First of them will be the number to be converted to hex, and second the custom scale, it will be optional and if it is supplied, it should be a string of exactly 16 characters otherwise the function returns false. The other two arguments are hexString and isNegative which are set to empty string and a boolean respectively by default.

Syntax

toHex(num, hexString, hex, isNegative)

Parameters

num - The decimal number to convert

hexString - Custom 16-character mapping string (optional, defaults to '0123456789ABCDEF')

hex - Internal parameter for recursion (defaults to empty string)

isNegative - Internal parameter to track negative numbers

Example

const num = 363;
const toHex = (
    num,
    hexString = '0123456789ABCDEF',
    hex = '',
    isNegative = num < 0
    ) => {
    if(hexString.length !== 16){
        return false;
    }
    num = Math.abs(num);
    if(num && typeof num === 'number'){
        //recursively append the remainder to hex and divide num by 16
        return toHex(Math.floor(num / 16), hexString,
        `${hexString[num%16]}${hex}`, isNegative);
    };
    return isNegative ? `-${hex}` : hex;
};

console.log(toHex(num, 'QWERTYUIOPASDFGH'));
console.log(toHex(num));
console.log(toHex(num, 'QAZWSX0123456789'));
WUS
16B
A05

How It Works

The function uses recursion to convert decimal to hexadecimal with custom character mapping:

  1. Validates that the custom hexString has exactly 16 characters
  2. Handles negative numbers by storing the sign and working with absolute value
  3. Uses modulo operation (num % 16) to get the remainder and map it to the custom character
  4. Recursively processes the quotient (Math.floor(num / 16)) until num becomes 0
  5. Builds the result string from right to left

Example with Different Mappings

// Standard hexadecimal
console.log(toHex(255)); // FF

// Custom mapping with letters only
console.log(toHex(255, 'ABCDEFGHIJKLMNOP')); // PP

// Custom mapping with numbers and symbols
console.log(toHex(255, '!@#$%^&*()+-=[]')); // ]]

// Handling negative numbers
console.log(toHex(-255)); // -FF
FF
PP
]]
-FF

Conclusion

This recursive approach provides flexible decimal-to-hexadecimal conversion with custom character mappings. The function maintains the mathematical logic of base-16 conversion while allowing personalized character sets for representation.

Updated on: 2026-03-15T23:18:59+05:30

442 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements