Base conversion is commonly used in JavaScript

Common base

The numbers that we encounter in our lives are based on the decimal system, which has to do with the fact that we have ten fingers on our hands, which is the way numbers are represented in human society, such as 0-9. However, in a computer, there are only two digits, 0 and 1, which are usually used to indicate the voltage level or the level of the electrical circuit, so all the digits and information in the computer are stored on the basis of binary, such as 0 and 1.

Usually, the numbers we are exposed to in computer programming are also in base 10, but when we encode data from network interfaces or file IO, the data we are exposed to is based on binary. For convenience, we usually convert binary data into hexadecimal for display, such as 0xABCDEF.

Therefore, this article introduces the common conversion between binary, octal, decimal, hexadecimal

Hexadecimal conversion

Hexadecimal conversion is mainly used in JavaScript Number. The parseInt () and Number. The prototype. The toString () method to implement, accept the parameters in the official document to convert hexadecimal Number

  • The number.parseint () method parses the string into integers based on the specified radix.

    Number.parseInt(string[, radix])
    Copy the code
  • Number. The prototype. The toString () method returns a string representation of the specified Number object.

    Number.prototype.toString([radix])
    Copy the code

binary

const binary = '0101010110';
Copy the code

Binary to octal

const octal = Number.parseInt(binary, 2).toString(8);
console.log(octal)
/ / 526
Copy the code

Binary to decimal

const decimal = Number.parseInt(binary, 2);
console.log(decimal);
/ / 342
Copy the code

Binary to hexadecimal

const hexadecimal = Number.parseInt(binary, 2).toString(16);
console.log(hexadecimal);
/ / 156
Copy the code

octal

const octal = '177';
Copy the code

Octal to binary

const binary = Number.parseInt(octal, 8).toString(2);
console.log(binary);
/ / 1111111
Copy the code

Convert octal to decimal

const decimal = Number.parseInt(octal, 8);
console.log(decimal)
/ / 127
Copy the code

Octal to hexadecimal

const hexadecimal = Number.parseInt(octal, 8).toString(16);
console.log(hexadecimal);
// 7f
Copy the code

The decimal system

const decimal = 255;
Copy the code

Decimal to binary

const binary = Number.parseInt(decimal, 10).toString(2);
console.log(binary);
/ / 11111111
Copy the code

Decimal to octal

const octal = Number.parseInt(decimal, 10).toString(8);
console.log(octal);
/ / 377
Copy the code

Conversion from decimal to hexadecimal

const hexadecimal = Number.parseInt(decimal, 10).toString(16);
console.log(hexadecimal);
// ff
Copy the code

hexadecimal

const hexadecimal = '0xfff';
Copy the code

Conversion from hexadecimal to binary

const binary = Number.parseInt(hexadecimal, 16).toString(2);
console.log(binary);
/ / 111111111111
Copy the code

Hexadecimal to octal

const octal = Number.parseInt(hexadecimal, 16).toString(8);
console.log(octal);
/ / 777
Copy the code

Conversion from hexadecimal to decimal

const decimal = Number.parseInt(hexadecimal, 16);
console.log(decimal);
/ / 4095
Copy the code

The other base

The other into the system, we can reference Number. The parseInt () and Number. The prototype, the toString () of redix to corresponding to convert hexadecimal Number

Source code has been posted to GitHub and can be downloaded and executed :-

More content

For more content, please pay attention to GitHub, Zhihu @Big Handprint, or follow my official account @full stack Developer. I will share some full stack development content from time to time, thanks for sharing

reference

Number

Number.parseInt()

Number.prototype.toString()