C Language Blog (6) conversion between programming basics

Source: Internet
Author: User

In my C language blog content, the previous two essays on the binary, octal and hexadecimal are described, and then tell how the different binary numbers are converted, which is often used in programming, especially the C language.

Other binary decimal conversions

In c language learning, binary, octal, and hexadecimal decimal conversions are very easy, that is, "add by weight."

The so-called "right", also namely "the right". For example, the 1th bit of the decimal right is 100=1, the 2nd bit right is 101=10, the 3rd bit of the right is 102=100, and the binary 1th bit right is 20=1, 2nd bit right is 21=2, 3rd bit right is 22=4. The binary used to set the number is N (cardinality is also N), then the bit right of the I bit is n (i-1).

The different binary conversions to decimal examples are:

    • Binary: 1001 = 1x23 + 0x22 + 0x21 + 1x20 = 8 + 0 + 0 + 1 = 9
    • Binary: 101.1001 = 1x22 + 0x21 + 1x20 + 1x2-1 + 0x2-2 + 0x2-3 + 1x2-4 = 4 + 0 + 1 + 0.5 + 0 + 0 + 0.0625 = 5.5625
    • Octal: 0302 = 3x82 + 0x81 + 2x80 = 192 + 0 + 2 = 194
    • Octal: 0302.46 = 3x82 + 0x81 + 2x80 + 4x8-1 + 6x8-2 = 192 + 0 + 2 + 0.5 + 0.09375 = 194.59375
    • Hex: 0xea7 = 14x162 + 10x161 + 7x160 = 3751
Decimal conversion to binary--rolling division

The table in the previous section gives a simple decimal and binary conversion relationship, and you can use rolling division to get more conversion relationships. The rolling division is the method of "removing the modulus from the remainder". In addition to the modulus of rest is the conversion of a number of a few into another, the base of the other is modulo, with the number of conversions to be converted by modulo, take its remainder.

Take the decimal "19" conversion to binary as an example to explain:


Figure 1:19 Converting to binary


, divide by 2, until the quotient is 0, the remainder is the binary number to be obtained.

Note: The remainder is ordered in reverse order, that is, the remainder of the first is ranked in the last side of the binary, and finally the remainder is ranked at the front of the binary. In the example above, the last binary number obtained is 10011.

While other binaries can be converted in terms of rolling division, it's a bit more cumbersome, and the simpler approach is described below.

Conversion of binary and octal binary

Binary to octal conversion is every three bits of binary converted to an octal number, the sequence of operations from the low to high order in turn, a high of less than three bits with 0 supplement. Take the binary "1011101" as an example, as shown in:


Figure 2: Binary turn octal


The result of the conversion is: 1011101 = 0135

The idea of octal to binary conversion is that an octal binary is converted to a binary three-bit, and the sequence of operations is from low to high. Also take the octal "0135" as an example, as shown in:


Figure 3: Octal binary to Binary


The result of the conversion is: 0135 = 1011101

Conversion of binary and hexadecimal

Binary to 16 conversion, four-bit conversion to 16 binary one, the sequence of operations is from low to high order, high, less than four bits with zero compensation. Take "1110011" to 16 as an example, as shown in:


Figure 4: Binary Turn hex


The result of the conversion is: 1001011101 = 0x25d

hexadecimal to binary conversion, is to convert a hexadecimal one into a binary four-bit, note that the order of operations from the low to high point in turn. Also take the hexadecimal "0x25d" as an example, as shown in:


Figure 5: Hex-to-binary this essay is a relatively basic C language learning, and will add a more professional essay on C programming or C language embedded development in the future as the study goes further. Also, I look forward to being able to grow in my studies.

C Language Blog (6) conversion between programming basics

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.