Conversion method: First use the parseInt() function to convert binary to decimal, the syntax is "parseInt(string,2);"; then use the toString() function to convert decimal to hexadecimal, the syntax is Format "number.toString(16)".
The operating environment of this tutorial: Windows 7 system, ECMAScript version 5, Dell G3 computer.
How to convert binary to hexadecimal in JavaScript
var a=100; var b=parseInt(a,2); b.toString(16)
Introduction to related functions:
The parseInt() function parses a string and returns an integer.
When the value of parameter radix is 0, or the parameter is not set, parseInt() will determine the base of the number based on string.
When the parameter radix is omitted, JavaScript defaults to the radix of numbers as follows:
If the string starts with "0x", parseInt() will parse the rest of the string into ten Hexadecimal integer.
If string starts with 0, then ECMAScript v3 allows an implementation of parseInt() to parse the following characters as octal or hexadecimal digits.
If string starts with a number from 1 to 9, parseInt() will parse it into a decimal integer.
Syntax
parseInt(string, radix)
toString() function can convert a Number object into a string and return the result.
Grammar
number.toString(radix)
Base conversion:
1. Decimal and binary, octal and hexadecimal System conversion
var a=11
1. Convert decimal to binary
a.toString(2)
2. Convert decimal to octal
a.toString(8)
3. Convert decimal to hexadecimal
a.toString(16)
2. Binary, octal, hexadecimal and decimal conversion
1. Binary to decimal
var b=100; parseInt(b,2);
2. Octal to decimal
var c=100; parseInt(c,8);
3. Hexadecimal to decimal
var d=100; parseInt(d,16);
For more programming related knowledge, please visit: Programming Video! !
The above is the detailed content of How to convert binary to hexadecimal in JavaScript. For more information, please follow other related articles on the PHP Chinese website!