Home >Web Front-end >JS Tutorial >How to convert binary to decimal in javascript

How to convert binary to decimal in javascript

青灯夜游
青灯夜游Original
2022-02-17 18:58:206662browse

In JavaScript, you can use the parseInt() function to convert a binary value to a decimal value. This function can parse a string with a specified base (base) and return a decimal value. Integer; syntax "parseInt("binary string",2)".

How to convert binary to decimal in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In javascript, you can use the parseInt() function to convert binary values ​​to decimal values.

Syntax:

parseInt(stringNum,[radix])

The stringNum parameter is a string that needs to be converted to an integer; the radix parameter is a number between 2 and 36, indicating the base number of the stringNum parameter, and the value It can be omitted when it is 10.
The function of parseInt() is to parse the stringNum string parameter with radix as the base into a decimal number. If the stringNum string does not start with a legal character, NaN will be returned; if an illegal character is encountered during the parsing process, parsing will stop immediately and the parsed value will be returned.

Example:

console.log(parseInt("101110100",2));//转成十进制
console.log(parseInt("1101",2));//转成十进制

How to convert binary to decimal in javascript

[Related recommendations: javascript learning tutorial]

The above is the detailed content of How to convert binary to decimal in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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