How To Convert Number To Hexadecimal In JavaScript

Convert Number to Hexadecimal in JavaScript

This post will demonstrate how to easily convert number to hexadecimal in Javascript. We have some methods can do it such as use the object.toString() or toString(16) function. Let’s see the specific steps are below.

Convert Number To Hexadecimal In JavaScript

Use Object.toString() function

A decimal number can be changed to hexadecimal using the toString() method. This approach’s primary goal is to achieve that. Nevertheless, since it is officially supported, the programmer can use it without performing a manual conversion.

The syntax is quite straightforward:

object.toString()

By default, no parameters are required. The statement mentioned above only returns the string representation of the Object instance, one of the built-in data types in JavaScript. In this circumstance, we are unable to use this.

For instance, this is how the toString() method is invoked most of the time:

const yourNumber = 42;
console.log(yourNumber);

Output:

42

The string that precisely represents the original integer is all that the example mentioned above provides. Fortunately, you may use optional parameters to alter the foundation of the numeric system if you call the toString() function on objects that derive from the Object type, such as Number.

In particular, if you already have a Number object, you can use the radix parameter to specify the base used for the string representation.

Employ the toString(16) function

In the “toString()” function, give the toString() function the radix parameter 16 as the base to convert to hexadecimal. Because the toString method accepts the base as an argument and returns the string representation of the number.

Example

Example 1: Using variable: object.toString(16)

// Declare the variable "yournumber" and give it the value "42".
const yourNumber = 42;

// Declare the variable "hexString" and give it the converted "yourNumber" value
const hexString = yourNumber.toString(16);

// Display the value of hexString to the console
console.log(hexString);

Or :

function numberToHex(number)
{
 const hexString = number.toString(16);
 return console.log(hexString);
}

numberToHex(42)

Output:

2a

Example 2: Calling toString directly: (number).toString

const hex = (42).toString(16);
console.log(hex);

Be aware that you must surround the number literal in parentheses if you call the toString method on it directly.

Output:

2a

Example 3: Convert a negative number to hexadecimal

Using the zero-fill-right shift operator:

function numberToHex(number) 
{
 return console.log(((number)>>>0).toString(16));
}

numberToHex(-42)

Output: 

ffffffd6

And we can parse back to number by parseInt(string,radix) function:

const hex = (42).toString(16);
const parse = parseInt(hex, 16);
console.log(parse);

Output: 

42

Summary

The above method deals with convert number to hexadecimal in JavaScript. The toString(16) function method will help you do it rapidly. If you have any questions or helpful methods, feel free to leave them in the comment below, and don’t forget to wait for our new articles in LearnShareIt.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *