Conversion between hexadecimal in C Language

Source: Internet
Author: User
Tags decimal to binary

1. 10 -----> 2
(10, 25.625) (10)

Integer:

25/2 = 12 ...... 1

12/2 = 6... 0

6/2 = 3... 0

3/2 = 1... 1

1/2 = 0... 1

Then we write the remainder in the order from bottom to bottom: 11001, then this 11001 is the binary form of decimal 25

Decimal part:

0.625*2 = 1.25

0.25*2 = 0.5

0.5*2 = 1.0

Then we write the integer part in the order from top to bottom: 101, then this 101 is the binary form of decimal 0.625

So: (25.625) (10) = (11001.101) (2)

Convert decimal to binary as follows:

Perform the division operation on the decimal number and overwrite the obtained remainder.

For example, convert decimal 10 to binary as follows:

(1) 10/2, with more than 5 vendors 0;

(2) 5/2, with more than 2 vendors and 1;

(3) 2/2, with more than 1 merchant 0;

(4) 1/2, with more than 0 vendors.

(5) The remainder percentile is 1010, so the decimal 10 is converted to binary 1010.

2. II ----> 10

(11001.101) (2)

Integer: The following 2 (x) indicates the meaning of 2 to the power of X.

1*2 (4) + 1*2 (3) + 0*2 (2) + 0*2 (1) + 1*2 (0) = 25

Decimal part:

1*2 (-1) + 0*2 (-2) + 1*2 (-3) = 0.625

So: (11001.101) (2) = (25.625) (10)

The binary conversion to decimal is like this:

Here we can use the 8421 code method. this method is to convert the binary value from right to left and start from 0 (This number is called N) and stop where the number of digits is 1, multiply 1 by the Npower of 2, and then multiply the Npower of 1 by the Npower of 2, which is the decimal of the binary number.

Let's take an example:

Calculates the decimal number of 110101. It starts from right to left.

(1) Multiply 1 by the power 0 of 2, and equal to 1;

(2) Multiply 1 by 2 to the power of 4;

(3) Multiply 1 by the 4 power of 2, which is equal to 16;

(4) Multiply 1 by the power of 2, which is equal to 32;

(5) add these results: 1 + 4 + 16 + 32 = 53

3. 10 ----> 8

(10, 25.625) (10)

Integer:

25/8 = 3... 1

3/8 = 0... 3

Then we write the remainder in the order from the bottom up: 31, then this 31 is the octal form of decimal 25

Decimal part:

0.625*8 = 5

Then we write the integer part in the order from top to bottom: 5, then this 0.5 is the octal form of decimal 0.625

So: (25.625) (10) = (31.5) (8)

4. 8 ----> 10

(1, 31.5) (8)

Integer:

3*8 (1) + 1*8 (0) = 25

Decimal part:

5*8 (-1) = 0.625

So (31.5) (8) = (25.625) (10)

5. 10 ----> 16

(10, 25.625) (10)

Integer:

25/16 = 1 ...... 9

1/16 = 0... 1

Then we write the remainder in the order from bottom to top: 19. Then 19 is the hexadecimal form of decimal 25.

Decimal part:

0.625*16 = 10 (that is, hexadecimal A or)

Then we write the integer part in the order from top to bottom: A, then this a is the hexadecimal form of decimal 0.625

So :( 25.625) (10) = (19.a) (16)

6. 16th ----> 10

(19.a) (16)

Integer:

1*16 (1) + 9*16 (0) = 25

Decimal part:

10*16 (-1) = 0.625

So (19.a) (16) = (25.625) (10)

How to convert decimal binary to octal and hexadecimal numbers

Let's take (11001.101) (2) as an example to explain the conversion problem between the hexadecimal notation.

Note: Secondary Computer Level 2 conversion in decimal part is not tested. If you are interested, you can take a look.

1. II ----> 8

(11001.101) (2)

Integer part: each group of three digits from the back to the front is filled with 0 in the absence, and then converted in decimal format, there are:

001 = 1

011 = 3

Then we write the result in the order from bottom to bottom: 31, then the 31 is the octal form of binary 11001.

Decimal part: each group of three digits from the past to the future. If there is no such group, fill it with 0 and convert it in decimal format:

101 = 5

Then we write the result part in the order from top to bottom: 5, then this 5 is the binary 0.101 octal form

So: (11001.101) (2) = (31.5) (8)

2. 8 ----> 2

(1, 31.5) (8)

Integer part: convert each digit from the back to the three-digit binary number in decimal format. If the value is left blank, the following values are used:

1 ----> 1 ----> 001

3 ----> 11

Then we will write the result in the order below: 11001, then this 11001 is the binary form of octal 31

Note: I will not talk about the decimal conversion method here. I have already explained it in the previous article!

Decimal part: Each digit from the past to the next is converted into three-digit binary numbers in decimal format. If the decimal number is absent, the following values are used:

5 ----> 101

Then we will write the result in the order below: 101, then this 101 is the binary form of octal 5

So: (31.5) (8) = (11001.101) (2)

3. 16th ----> 2

(19.a) (16)

Integer: convert each digit from the back to the four-digit binary number in decimal format. If the value is left blank, the following values are used:

9 ----> 1001

1 ----> 0001 (equivalent to 1)

The result is 00011001 or 11001.

Decimal part: from the past to the next, each digit is converted to a four-digit binary number in decimal format. If the number is left blank, the following values are used:

A (10) ----> 1010

So :( 19.a) (16) = (11001.1010) (2) = (11001.101) (2)

4. II ----> 16

(11001.101) (2)

Integer part: convert every four digits from the back to a single digit in decimal format. If there is no integer, add 0:

1001 ----> 9

0001 ----> 1

The result is 19.

Decimal part: convert every four digits from the past to the next to a single digit in decimal format. If the decimal part is left blank, the following values are added:

1010 ----> 10 ---->

The result is.

So :( 11001.101) (2) = (19.a) (16)

[Edit this section]

Ii. Negative number

The hexadecimal conversion of negative numbers is slightly different.

First, write the negative number as its complement form (this is not discussed here), and then convert other hexadecimal methods based on the binary.

For example, convert-9 to octal format. There are:

The complement code of-9 is 11111001. Then, three rows are marked.

001 ----> 1

111 ----> 157

011 ----> 3

Then we will write the result in the order below: 31571, then 31571 is the octal form of the decimal number-9.

Supplement:

Recently, some friends raised the question "What Is The hexadecimal 0.8 ?"

I want to explain in detail in my space. Why does he need to ask such a question?

So I figured it out and found that some numbers are converted between the 0.8, 0.6, and 0...

There is indeed a problem in the process.

For example, "hexadecimal 0.8!

No matter how you multiply by 16, the remainder of it is also not enough, always more than 8

The specific method is as follows:

0.8*16 = 12.8

0.8*16 = 12.8

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.