• notice
  • Congratulations on the launch of the Sought Tech site

JavaScript binary, octal, hexadecimal

When we talked about strict mode in the previous section, we mentioned that octal numbers cannot be used in strict mode. In this section, let’s talk about what is binary, octal, and hexadecimal.

Decimal

The decimal number is the number represented by the ten numbers of 0, 1, 2....9. Decimal is a number system based on 10, which is the most widely used carry system in the world. When counting according to the "every decimal one" rule, every ten identical units form a higher unit adjacent to it.This counting method is called decimal notation, or decimal for short.

In fact, what we learn in mathematics is the decimal system, and most of the decimal systems are used in our daily life.For example, the common numbers such as 1, 2, 3, 7, 28, 47, 100, etc., are the decimal system.

JavaScript supports converting decimal values into binary, octal, and hexadecimal values.

Binary

Binary is a number system widely used in computing technology. Binary data is a number represented by two digits, 0 and 1. Its base is 2, the carry rule is "every two enters one", and the borrow rule is "borrow one to become two".

We just have to remember that binary is only a combination of 0 and 1.For example, a number like 110011 is a binary number.Different combinations represent different values.But if there are numbers other than 0 and 1, then this number is It's not binary.

Example:

Decimal to binary:

var a = 100; // this is a decimal
result = a.toString(2); // decimal to binary
console.log(result);
// Output: 1100100

Binary to decimal:

var n = "1100100" // This is a binary string
result = parseInt(n, 2); // binary to decimal
console.log(result);
// Output: 100

Octal

Octal is a counting method with 8 as the base, using 0, 1, 2, 3, 4, 5, 6, 7 eight digits, and 1 is entered every eight. Some programming languages often start with the number 0 to indicate that the number is octal. Octal numbers and binary numbers can correspond to each other bit by bit (one bit in octal corresponds to three bits in binary), so they are often used in computer languages.

To be more specific, it starts with the number 0.The eight numbers between 0 and 7 are octal.For example, 075 is an octal number.

Example:

Convert decimal to octal:

var a = 100; // this is a decimal
result = a.toString(8); // convert to octal
console.log(result);
// Output: 144

Octal to decimal:

var n = "056" // This is an octal string
result = parseInt(n, 8); // octal to decimal
console.log(result);
// Output: 46

Hexadecimal

Hexadecimal is a way of representing data in a computer. It is not the same as the decimal notation in our daily life. It is generally represented by numbers 0 to 9 and letters A to F (or a~f).Any combination of these numbers and letters is used to represent a word between 0 and 15. Among them, A~F represent 10~15, and these are called hexadecimal numbers.

In mathematics, hexadecimal is a rounding system of "advancing every hexadecimal, borrowing a hexadecimal", for example, the numbers 0, 3, 6, 9, A, D, F, 419, EA32, 80A3, BC00 are all valid hexadecimals.

Example:

Convert decimal to hexadecimal:

var a = 100; // this is a decimal
result = a.toString(16); // convert to hexadecimal
console.log(result);
// Output: 64

Hexadecimal to decimal:

var n = "AF64" // This is a hexadecimal string
result = parseInt(n, 16); // hexadecimal to decimal
console.log(result);
// Output: 44900

Give it a try

  1. What is the conversion of binary 10101010 to decimal?

  2. Please convert the decimal number 38 to binary, octal and hexadecimal respectively?

  3. Please convert hexadecimal F60 to decimal?


Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

lipitor online buy & lt;a href="https://lipiws.top/"& gt;buy lipitor 10mg pills& lt;/a& gt; atorvastatin 80mg generic

Zaglrj

2024-03-10

Leave a Reply

+