# Javascript convert hex to decimal, binary to hex, octal in turn

Lionsure 2020-05-20 Original by the website

Conversion between systems is a common thing in the process of writing a program. Various programming languages have the conversion methods from a system to another system, some provide direct conversion methods, and some need to write conversion codes. Javascript provides a conversion method, and directly calls two functions to achieve conversion between the systems.

Commonly used hexadecimal conversion: hex to decimal or decimal to hex, binary to decimal, hex, octal, octal to decimal, hex, etc. Then look at how to use javascript to realize these conversion between systems.

### I. Conversion function (method) of system in Javascript

The "object" is a conversion object; the "radix" is the system number to be converted to.

The "object" is a required option, it is a conversion object.

The "radix" is optional, and means the system of object, the range is from 2 to 36; if the "radix" is missing, the character string starting with 0x is converted to hexadecimal, the character string starting with 0 is converted to octal, all other strings decimal.

### II. Javascript convert hex to decimal

var x = 0x20;
x.toString(10);//Convert hex to decimal

Output: 32

Or:

parseInt(x, 10)

Output: 32

### III. Javascript decimal to hex

var x = 20;
x.toString(16);//Decimal to hex

Output: 14

### IV. Javascript convert binary to decimal, hex and octal

var x = 110;
parseInt(x, 2);//Convert binary to decimal

Output: 6

Convert Binary to hex javascript

var x = 10111100;
x = parseInt(x, 2);//Convert binary to hex
x = x.toString(16);//Convert decimal to hex

Output: bc

Javascript convert binary to octal

var x = 10111100;
x = parseInt(x, 2);//Convert binary to octal
x = x.toString(8);//Convert decimal to octal

Output: 274

Javascript convert decimal to binary

var x = 7;
x.toString(2);//Convert decimal to binary

Output: 111

var x = 0xa;
x.toString(2);//Convert hex to binary

Output: 1010

var x = 032;
x.toString(2);//Convert octal to binary

Output: 11010

### V. Javascript octal to decimal, hex

var x = 032;
x.toString(10);//Octal to decimal

Output: 26

var x = 032;
x.toString(16);//Octal to hex

Output: 1a

var x = 32;
x.toString(8);//Decimal to octal

Output: 40

var x = 0x32;
x.toString(8);//Hex to octal

Output: 62

In fact, system conversion uses toString() and parseInt() two methods in javascript, as long as you know how to represent hexadecimal, octal and binary numbers, just call them.