PHP basic tutorial

Nov 21, 2016 am 09:32 AM

Brothers PHP training (www.lampbrother.net) Detailed explanation: Binary, octal, decimal, and hexadecimal conversion 1. Conversion between decimal and binary

    (1) Decimal to binary conversion, divided into integer part and decimal part Part

 ① Integer part

Method: divide by 2 and take the remainder, arrange in reverse order, that is, divide the integer part by 2 each time, the remainder is the number on the digit, and the quotient continues to be divided by 2, and the remainder is the previous digit. For the number on the weight, this step continues until the quotient is 0. When reading the final number, read from the last remainder to the first remainder. Here is an example:

Example: Convert decimal 168 to binary

The result is Convert decimal 168 to binary, (10101000)

The first step is to divide 168 by 2, the quotient is 84, and the remainder is 0.

 The second step is to divide the quotient 84 by 2, and the remainder of the quotient 42 is 0.

 The third step is to divide the quotient 42 by 2, and the remainder of the quotient 21 is 0.

The fourth step is to divide the quotient 21 by 2, and the remainder of the quotient 10 is 1.

The fifth step is to divide the quotient 10 by 2, and the remainder of the quotient 5 is 0.

The sixth step is to divide the quotient 5 by 2, and the remainder of the quotient 2 is 1.

Step 7: Divide quotient 2 by 2, and the remainder of quotient 1 is 0.

Step 8: Divide the quotient 1 by 2, and the remainder of the quotient 0 is 1.

 Step 9, reading, because the last digit is obtained after dividing by 2 many times, so it is the highest digit. Read the number forward from the last remainder, that is, 10101000

 (2) Decimal part

Method: Multiply by 2 and round to an integer, arrange in order, that is, multiply the decimal part by 2, then take the integer part, continue to multiply the remaining decimal part by 2, then take the integer part, and multiply the remaining decimal part by 2, until Until the decimal part

is zero. If it can never be zero, it is the same as the rounding of decimal numbers. When retaining as many decimal places as required, the number will be rounded based on whether the next digit is 0 or 1. If it is zero, round it off. If it is 1, add one digit. In other words, 0 is rounded to 1. The reading should be read from the previous integer to the following integer. Example 1: Convert 0.125 to binary

Multiply by 2 to get 0.25, then the integer part is 0 and the decimal part is 0.25;

  The second step is to multiply the decimal part 0.25 by 2 and get 0.5, then the integer part is 0 and the decimal part is 0.5;

  Third Step 1: Multiply the decimal part 0.5 by 2 to get 1.0, then the integer part is 1 and the decimal part is 0.0;

  The fourth step is to read from the first digit to the last digit, which is 0.001.

 Example 2, convert 0.45 to binary (retained to the fourth decimal place)

  As you can see from the above steps, when the fifth multiplication is performed, the result obtained is 0.4, then the decimal part continues to be multiplied by 2, We get 0.8, multiply 0.8 by 2, and keep multiplying until 1.6. In the end, it is impossible to get the decimal part to be zero. Therefore, at this time, we have to learn the decimal method to round, but binary only has 0 and 1, so The occurrence of 0 is rounded to 1. This is also caused by computer errors during conversion, but since there are many reserved digits and the accuracy is very high, it can be ignored.

  Then, we can conclude that converting 0.45 to binary is approximately equal to 0.0111

  The method introduced above is a method of converting decimal to binary. What everyone needs to pay attention to is:

  1) To convert decimal to binary, it needs to be divided into integers and decimal parts are converted separately

  2) When converting integers, the division by 2 method is used to round up the remainder, while when converting decimals, the multiplication by 2 rounding method is used

  3) Pay attention to their reading direction

  Therefore, From the above method, we can conclude that the decimal number 168.125 converted to binary is 10101000.001, or the decimal number converted to binary is approximately equal to 10101000.0111.

 (3) Convert binary to decimal without dividing into integers and decimal parts

 Method: Add by weight, that is, multiply the number in each bit of the binary by the weight, and then add the sum to the decimal number. Example

 Convert the binary number 101.101 to a decimal number.

 The result is: (101.101)2=(5.625)10

What everyone needs to pay attention to when converting binary to decimal is

 1) You must know the weight of each binary digit

 2) You must be able to calculate each bit The value of

2. Conversion between binary and octal

First, we need to understand a mathematical relationship, that is, 23=8, 24=16, and octal and hexadecimal are derived from this relationship

, that is, three binary digits are used to represent an octal number, and four binary digits are used to represent a hexadecimal number.

Next, remember the four numbers 8, 4, 2, 1 (23=8, 22=4, 21=2, 20=1). Now let's practice converting between binary and octal.

 (1) Convert binary to octal

Method: Take the three-in-one method, that is, starting from the decimal point of the binary system as the dividing point, take every three digits to the left (right) into one digit, and then add these three binary digits according to weight, and the resulting number is a one-digit, eight-digit binary system. The numbers are then arranged in order, the position of the decimal point remains unchanged, and the number obtained is the octal number we are looking for. If you take three digits to the left (right) and get to the highest (lowest) digit, if you cannot make up the three digits, you can add 0 to the leftmost (rightmost) of the decimal point, that is, the highest (lowest) digit of the integer. Make up three people. Example

 ①Convert the binary number 101110.101 to octal

 Get the result: Convert 101110.101 to octal to 56.5

 ② Convert the binary number 1101.1 to octal

 Get the result: Convert 1101.1 to The octal system is 15.4

 (2) Convert octal to binary

Method: Take the one-third method, that is, decompose an octal number into three binary digits, and use the three-digit binary digits to add the octal number according to the weight. The decimal point position remains the same. Example:

 ① Convert the octal number 67.54 to binary

Therefore, convert the octal number 67.54 to the binary number 110111.101100, which is 110111.1011

As you can see from the above question, calculate the conversion of octal to binary

 First of all, Expand each octal number from left to right to three digits, leaving the decimal point unchanged

Then, expand each digit into three digits: 22, 21, 20 (i.e. 4, 2, 1) to make up the number, that is, a× 22+ b×21 +c×20=the number in the bit (a=1 or a=0, b=1 or b=0, c=1 or c=0), arrange abc to get the binary number of the bit

 Then, convert each bit into a binary number and arrange them in order

 Finally, you will get the number converted from octal to binary.

 The above method is the exchange of binary and octal. What you need to pay attention to when doing the questions is

 1) The exchange between them is the conversion between one digit and three digits, which is different from the conversion between binary and decimal.

  2) When adding 0 and removing 0, please note that you must add 0 or remove 0 at the far left of the decimal point or the far right of the decimal point (that is, the highest digit of the integer and the lowest digit of the decimal), otherwise an error will occur.

  3. Binary to hexadecimal conversion

Method: Similar to binary to octal conversion, it is just a conversion of one digit (hexadecimal) and four digits (binary). The following will explain in detail

  (1) Binary Convert to hexadecimal

Method: Take the four-in-one method, that is, use the binary decimal point as the dividing point, take every four digits to the left (right) into one, and then add these four binary digits according to weight to get The number is a sixteen-digit binary number, and then, arranged in order, the position of the decimal point remains unchanged, and the number obtained is the hexadecimal number we are looking for. If you take four digits to the left (right) and get to the highest (lowest) digit, if you cannot make up the four digits, you can add 0 to the leftmost (rightmost) of the decimal point, that is, the highest (lowest) digit of the integer. Make up four people.

 ①Example: Convert binary 11101001.1011 to hexadecimal

 Get the result: Convert binary 11101001.1011 to hexadecimal to E9.B

 ②Example: Convert 101011.101 to hexadecimal

 Therefore get results : Convert binary 101011.101 to hexadecimal into 2B.A

  (2) Convert hexadecimal to binary

 Method: Take the one-quarter method, that is, decompose a hexadecimal number into four binary digits Number, use four binary digits to add up the hexadecimal number, and the decimal point position remains the same.

  ① Convert hexadecimal 6E.2 to binary number

Therefore, the result is: Convert hexadecimal 6E.2 to binary to 01101110.0010, which is 110110.001

4. Conversion of octal and hexadecimal

Method: Generally, they cannot be converted directly to each other. Generally, octal (or hexadecimal) is converted to binary, and then binary is converted to hexadecimal (or octal). The decimal point position remains unchanged. Then for the corresponding conversion, please refer to the above conversion of binary to octal and conversion of binary to hexadecimal

5. Conversion of octal to decimal

(1) Conversion of octal to decimal

Method: Press the weighted addition method, that is, The numbers in each bit of the octal system are multiplied by the bit weights and then added together to get the decimal number.

 Example: ①Convert the octal number 67.35 to decimal

 (2) Convert decimal to octal

There are two methods to convert decimal to octal:

 1) Indirect method: first convert decimal to binary, and then convert binary again Convert to octal

 2) Direct method: As we said before, octal is derived from binary, so we can use a similar method to convert decimal to binary, or convert the integer part and the decimal part, as follows Let’s explain it in detail:

 ①Integer part

Method: Divide by 8 and take the remainder method, that is, divide the integer part by 8 each time, the remainder is the number on the place weight, and the quotient continues to be divided by 8, the remainder is the number on the previous place weight, and this step continues , until the quotient is 0. When reading the last number, start from the last remainder to the first remainder.

  ② Decimal part

Method: Multiply by 8 to round up the whole method, that is, multiply the decimal part by 8, then take the integer part, continue to multiply the remaining decimal part by 8, then take the integer part, and multiply the remaining decimal part by 8. Take until the decimal part is zero. If it can never be zero, it is the same as the rounding of decimal numbers, temporarily named 3 rounding.

  Example: Convert the decimal number 796.703125 to octal number

Solution: First divide this number into the integer part 796 and the decimal part 0.703125

The integer part

The decimal part

Therefore, we get the result decimal 796.70 Convert 3125 to octal to 1434.55

You can verify the above method. You can first convert decimal and then convert to octal to see if the results are the same

6. Conversion between hexadecimal and decimal

There are differences between hexadecimal and octal There are many similarities. You can refer to the above conversion between octal and decimal to try the conversion between these two systems.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles