Home Web Front-end JS Tutorial Using JavaScript for base conversion to convert string to decimal_javascript tips

Using JavaScript for base conversion to convert string to decimal_javascript tips

May 16, 2016 pm 04:35 PM
decimal Base conversion

JS is a very magical language, with many built-in functions that can help us convert numbers (base);

Hexadecimal can be used directly in JS;
var a = 0xff; //255

Convert any base string to decimal, such as binary, octal, hexadecimal. The most commonly used conversion is to integer decimal without writing the second digit;

Copy code The code is as follows:

parseInt("11", 2); // 3 binary to decimal
parseInt("77", 8); // Convert 63 from octal to decimal
parseInt("af", 16); //175 hexadecimal to decimal

Convert decimal to binary, octal, hexadecimal string
Object.toString(n): (n) represents the base, such as

Copy code The code is as follows:

(152).toString(2) // "10011000" ; First use brackets to "package" 152 into an object, or write it as follows;
152..toString(2) // The first point here converts 152 into a float type decimal, and the second point is to introduce the object method;
152..toString(16) // "98" : Convert decimal to hexadecimal
152..toString(32) // "4o": convert decimal to 32

Similarly, the maximum base supported by Javascript is 36 (26 English letters and 10 numbers)

35..toString(36) // "z" : supports the maximum encoding "Z", not case sensitive

If it needs to be completed during the conversion process. You can use the following methods:

Copy code The code is as follows:

/**
* @param num The 16 numbers that need to be completed
* @param len The number of digits to be filled in. This is
* @returns completed string
**/
function format(num, len) {
var l = num.length;
if (num.length < len) {
for (var i = 0; i < len - l; i ) {
num = "0" num;
}
}
return num;
}
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to convert hexadecimal string to number in php How to convert hexadecimal string to number in php Oct 26, 2021 pm 06:36 PM

How to convert a hexadecimal string to a number in PHP: 1. Use the hexdec() function with the syntax "hexdec(hexdecimal string)"; 2. Use the base_convert() function with the syntax "bindec(hexadecimal string)" String, 16, 10)".

How to use the CONV function in MySQL to convert a numerical value to a different base How to use the CONV function in MySQL to convert a numerical value to a different base Jul 12, 2023 pm 02:33 PM

How to use the CONV function in MySQL to convert a value to a different base Introduction: In the database, it is often necessary to convert a value between different bases. MySQL provides a very convenient function CONV, which can quickly realize hexadecimal conversion of numerical values. This article details how to use the CONV function and provides some code examples. 1. Overview of CONV function The CONV function is a mathematical function provided by MySQL, which is used to convert a value from one base to another. its

What is the main reason for using binary within computers? What is the main reason for using binary within computers? Apr 04, 2019 pm 02:25 PM

The main reasons why computers use binary systems: 1. Computers are composed of logic circuits. Logic circuits usually only have two states, the switch is on and off, and these two states can be represented by "1" and "0"; 2. Only two numbers, 0 and 1, are used in the binary system, which is less error-prone during transmission and processing, thus ensuring high reliability of the computer.

How to convert binary to decimal in JavaScript? How to convert binary to decimal in JavaScript? Aug 26, 2023 pm 02:21 PM

In this tutorial, we will learn to convert binary to decimal in JavaScript. Binary numbers are used in digital electronics. It is a string consisting of '0' and '1', representing a number relative to base 2. Following are the different ways to convert binary numbers to decimal numbers. Using parseInt() Method In JavaScript, the parseInt() method is very useful for extracting numbers from strings. We can define the base of a number as a parameter in the parseInt() method. Syntax Users can use the parseInt() method to convert binary to decimal by following the following syntax. letbinary="0101";le

How to convert decimal to binary in c language How to convert decimal to binary in c language Nov 24, 2022 am 10:27 AM

How to convert decimal to binary: Keep dividing the decimal number by 2 until the quotient is zero, and then write the remainder from bottom to top; the conversion code "int main(void){int n,len;int a[20] ;scanf("%d",&n);while(n/2){a[len++]=n%2;n=n/2;}a[len++]=n%2;for(i=len-1 ;i>=0;i--){printf("%d",a[i]);}}".

C program to convert decimal to binary? C program to convert decimal to binary? Sep 08, 2023 pm 05:45 PM

Convert an integer from decimal (base-10) to binary (base-2). Assuming the size of the integer is 32 bits, the number needs to be divided by the base. It is used by computers to change integer values ​​into bytes for the computer. Input:10Output:1010 indicates that if the decimal number is 1010 divided by 2, the remainder is zero. Therefore, 0. Divide 10 by 2. The new number is 10/2=5. When dividing by 5, the remainder is 1. So it's 1. Divide 5 by 2. The new number is 5/2=2. When 2 is divided by 2 the remainder is zero. Therefore, 0. Divide 2 by 2. The new number is 2/2=1. When 1 is divided by 2 the remainder is 1. Therefore, it is 1. Divide 1 by 2. The new number is 1/2=0. number becomes =0. in reverse order

Implementation of C language program for converting decimal to binary Implementation of C language program for converting decimal to binary Sep 06, 2023 pm 11:09 PM

Question How to convert decimal number to binary number using function in C language? The solution is that in this program, we call a binary function in main(). The binary number conversion function called will perform the actual conversion. The logic of the calling function we use to convert decimal numbers to binary numbers is as follows - while(dno!=0){ rem=dno%2; bno=bno+rem*f; f=f*10; dno=dno/2 ;} Finally, the binary number

Java program: hexadecimal to decimal conversion Java program: hexadecimal to decimal conversion Aug 26, 2023 pm 01:01 PM

There are four types of number systems: binary, octal, decimal, and hexadecimal, with base values ​​2, 8, 10, and 16 respectively. The base value depends on the number of digits the number system contains. For example, the binary number system contains only two digits, 0 and 1, so its base is 2. In this article, we will discuss hexadecimal and decimal number systems. Also, we will write java program to convert hexadecimal number to decimal number. Hexadecimal and Decimal Number Systems Hexadecimal Number System It represents the numbers from 0 to 9, A to F. There are 16 numbers in total, and its base value is also 16. The weight of individual numbers is a power of 16, so each number is 16 times heavier than the previous one. 12A16, 34B16, 45C16 are a few examples of hexadecimal. In a computer,

See all articles