Number conversion (binary, octal, decimal, and hexadecimal)

Source: Internet
Author: User

Because the computer only has high and low levels and can only represent 0 and 1 states, binary is generated, and people are used to decimal digits. Therefore, there is a conversion between the decimal and binary. However, since the binary representation is not very good (especially when the value is large), it is troublesome to convert the decimal to the binary, the octal and hexadecimal formats are generated.

Base table


It is easy to convert binary into octal and hexadecimal.

Every progress is yes. Every digit has the right to do so. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + yOejujEwMDGjqLb + weight/dfWyOe5 + weight/2z8Kx7cq + tcTK/Weight = "brush: java;"> # include # Define Binary 0b100 # define Octal 0100 # define Decimal 100 # define Hex 0x100int main () {printf ("the Binary % d \ n", Binary ); printf ("the Octal % d \ n", Octal); printf ("the Decimal % d \ n", Decimal); printf ("the Hex % d \ n ", hex); return 0 ;}

Running result:

The Binary 4
The Octal 64
The Decimal 100
The Hex 256
The default value in C is decimal. If a value starts with 0, it is octal. If it starts with 0x or 0X, It is hexadecimal.

Similarly, you can use printf to specify different formats to print the corresponding hexadecimal format.

% D % u printed in decimal notation

% O octal Printing

% X % X hexadecimal printing. The difference between the two is that x uses abcedf, while X uses ABCDEF.


Binary and octal/hexadecimal conversions are relatively simple, because they are the 3rd power of 2 and the 4th Power.

For example:

1100 (Binary) -----> 001100 ----> 14 (octal)

1100 (Binary) -----> 1100 ----> c (hexadecimal)

The opposite is true.


Decimal and Binary Conversion

Decimal ------> binary

Here, the hexadecimal format can be used too much. 16,256,409 6, 65536,104 8576, 16777216... (they are 16 ^ 1, 16 ^ 2, 16 ^ 3 ......)
First, find the first number that is smaller than the number to be converted, and then use this number to divide the number to be converted. The result is the number of that bit (the number of digits is the index ), then find the first number smaller than the remainder ,)

For example:

9999 ----> binary?

The 9999/4096 operator is 2, indicating that the hexadecimal 3rd bits are 2, and the remainder is 1807.

The 1807/256 operator is 7, indicating that the hexadecimal 2nd bits are 5, and the remainder is 15.

The first digit of the hexadecimal system is 0, and the second digit is 15.

9999 (decimal) ----> 270f (hexadecimal) -----> 0010 0111 0000 1111 (Binary)

In this way, the number as big as 9999 has been much faster than 2.

Binary Conversion to decimal is relatively simple. As mentioned above, it can be done by bitwise multiplication. Decimal to octal and hexadecimal are the same as above, but the steps for converting to binary are omitted.






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.