C language implementation of the binary conversion algorithm

Source: Internet
Author: User
Tags binary to decimal

The problem with getting into the system is almost something you learned when you started the computer, even so, do you know how the conversion is made between the binaries? Here is a detailed explanation of the conversion principle between the various systems.


(i) two, 86 or 16 binary decimal

2, 8, 16 ext. 10 There is a general algorithm, that is, the number of members multiplied by the radix of the n Power (n is the number of digits after the base number) is evaluated after the sum obtained. A little bit of a detour, huh? Chinese did not learn, please understand, the following example will be understood.

Eg:100002=?10

This is to convert the binary 10000 into decimal, from left to right, the first bit is 1, the trailing digits are 0, that is:

1*24+0*23+0*22+0*21+0*20=16+0+0+0+0=16

24 of 2 is the cardinality, and the last sum is the decimal.

Eg:57458=? 10

or according to the algorithm above:

5*83+7*82+4*81+5*80=3045

Did you get it? Look at the 16 binary, with the above two is different, with the letter in hexadecimal, not difficult to understand, met a to replace it with 10, met B to replace 11, and so on until F, or to see examples, casually write a number (it is really handy to write, do not believe you asked me the keyboard O (^▽^) o):

Eg:2435de16=?10

2*165+4*164+3*163+5*162+13*161+14*160=2373086

Code:


Binary to Decimal */* Blog: Http://blog.csdn.net/cnshsh */#include <stdio.h> #include <stdlib.h>//itoa () function header File # Include <string.h>int binary2decimal (int b) {char s[100];int re = 0,i,n;itoa (b,s,10);//The binary number that will be passed in as a string stores n = Strlen (s)-1;for (i=n; i>=0; i--) Re + = s[i]-48<<n-i; return re;} int main () {printf ("%d\n", Binary2decimal (1011));}




(b) Decimal to two, 86 or 16 binary


This needless to say, of course, there are their own general algorithm, the decimal number divided by the other binary cardinality, the remainder of the reverse order, to see the example

Eg:1010=? 2

10/2 =5 0

5/2 = 2 1

2/2 = 1 0

1/2 = 0 1

Last written in reverse: 1010

Eg:8310=? 8

83/8 = 10 3

10/8 = 1 2

1/8 = 0 1

Reverse: 123

Eg:29910=? 16

299/16 = 18 11

18/16 = 1 2

1/16 = 0 1

In reverse write more than 9 of the letter instead, such as this 11 is replaced by B: 12B

Write so much today, and have time to fill in the other two conversions.



C language implementation of the binary conversion algorithm

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.