Home > Web Front-end > JS Tutorial > How to convert binary to hexadecimal in JavaScript

How to convert binary to hexadecimal in JavaScript

青灯夜游
Release: 2023-01-03 09:31:04
Original
73425 people have browsed it

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)".

How to convert binary to hexadecimal in JavaScript

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)
Copy after login

How to convert binary to hexadecimal in JavaScript

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)
Copy after login

How to convert binary to hexadecimal in JavaScript

toString() function can convert a Number object into a string and return the result.

Grammar

number.toString(radix)
Copy after login

How to convert binary to hexadecimal in JavaScript

Base conversion:

1. Decimal and binary, octal and hexadecimal System conversion

var a=11
Copy after login

1. Convert decimal to binary

a.toString(2)
Copy after login

2. Convert decimal to octal

a.toString(8)
Copy after login

3. Convert decimal to hexadecimal

a.toString(16)
Copy after login

2. Binary, octal, hexadecimal and decimal conversion

1. Binary to decimal

var b=100;
parseInt(b,2);
Copy after login

2. Octal to decimal

var c=100;
parseInt(c,8);
Copy after login

3. Hexadecimal to decimal

var d=100;
parseInt(d,16);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template