| by Arround The Web | No comments

How to Convert Number Into Binary, Octal, or Hexadecimal Strings in JavaScript?

Binary”, “Octal”, and “Hexadecimal” are the common number systems used in computer science and digital electronics. There are several uses for these number systems in computer programming. For instance, “hexadecimal” is used to represent memory locations, color codes, and ASCII codes, and is also utilized in networking protocols, such as IPv6. Whereas, “Binary” is used as the fundamental/basic language of computers, whereas “octal” is used as a shorthand for binary.

This blog will illustrate the procedure for converting a decimal number to binary, octal, or hexadecimal strings in JavaScript.

How to Convert Numbers Into Binary, Octal, or Hexadecimal Strings in JavaScript?

For converting numbers to any number system, such as “binary”, “octal”, or “hexadecimal”, use the “toString()” method. It is a built-in method of the “String” object that takes a base of the number system as a parameter to convert the number into a specified base, such as “binary”, “octal”, or “hexadecimal”.

Syntax

Follow the given syntax for converting numbers into a binary, octal, or hexadecimal string:

number.toString(base)

Here, the base will be “2” for binary, “8” for octal, and “16” for hexadecimal number.

Approach 1: Convert Number Into Binary String

Binary is a base-2 number system, which means that all numbers are represented by only two digits, 0 and 1. For converting into binary, pass the base “2” as an argument in the “toString()” method.

Example

Create a variable “num” that stores the number “315”:

var num = 315;

Now, call the “toString()” method by passing base “2” to convert the number into binary number system:

var binaryNum = num.toString(2);

Finally, print the resultant binary number on the console:

console.log(binaryNum);

The output displays “100111011” which is the binary representation of the number “315”:

Approach 2: Convert Number Into Octal String

Octal is a base-8 number system that uses eight digits, 0 to 7. It is frequently utilized as a shorthand for binary. For instance, one octal digit can represent three binary digits. Although it is less commonly used than binary and hexadecimal, it is still utilized in some computer programming and digital systems.

Example

For converting a number into octal or base 8, pass the “8” as an argument to the “toString()” method:

var octalNum = num.toString(8);

console.log(octalNum);

Output

Approach 3: Convert Number Into Hexadecimal String

Hexadecimal or base-16 is a number system with 16 digits ranging from 0 to 9 and A to F. It is frequently used as a shorthand for binary and is widely utilized in computer programming.

Example

Call the “toString()” method with “16” as an argument for converting a number into a hexadecimal string:

var hexNum = num.toString(16);

Finally, print the hexadecimal number on the console:

console.log(hexNum);

It can be seen that the “315” has been successfully converted into a hexadecimal number that is “13b”:

That was all about the conversion of the number to the binary, octal, or hexadecimal number in JavaScript.

Conclusion

For converting numbers into binary, octal, or hexadecimal strings, utilize the “toString()” method. This method takes a base of the number system as a parameter for converting the number into a specified base, such as “binary”, “octal” or “hexadecimal”. This blog illustrated the procedure for converting a number to binary, octal, or hexadecimal strings in JavaScript.

Share Button

Source: linuxhint.com

Leave a Reply