Base conversion (white advanced road)

Hexadecimal conversion

White first to write blog, all before the title and code as well as knowledge in the form of a picture on the phone, but I did not find anything with , but has been lying on the phone, look at the code is not convenient, but also for knowledge is not deep impression, so try to write your own blog, the feeling will be deeper impression. .
I went to summer school and more training cattle off the network, and then every game autistic (trained, trained, trained), often burst zero (cry chirp). .
Without further ado, the question
method at Binary decimal yesterday just finished one game today complement binary conversion problems found or not it will, turned the solution to a problem under the big brother, summary

First, that the simplest method is the easiest to understand
Here Insert Picture Description
each index multiplied by 2 to the power of the binary, and then added together, this is relatively simple, directly on the code

    string s;
    int ans=0;
    cin>>s;
    int len=s.length();
    for(int i=0;i<len;i++)
    {
        ans+=pow(2,len-i-1)*(s[i]-'0');
    }
    cout<<ans<<endl;

Today Replenishment it was found Gangster code

string s;
cin>>s;
int len=s.length();
int ans=0;
for(int i=0;i<len;i++)
{
    ans=ans*2+s[i]-'0';
}
cout<<ans<<endl;

At first not understand how, but later thought for a moment, if the count is not the result before ans, directly into the next calculation, the equivalent of the binary number are each multiplied by the corresponding index of power of 2, that and a method of thought on the same subject.

PS:
How quickly for binary and decimal conversion
first to remember the power of the exponent 2:
2,4, 8,16, 32, 64, 128, 256, 512 1024
. 1. 1
10 2
100. 4
1000. 8
10000 16
100000 32
1000000 64
10000000 128
100000000 256
1000000000 512
100,000,000,001,024

Remember these words, will quickly calculate 11101
equivalent to 16 + 8 + 4 + 1 = 29 ( how I feel about the same and a method for the EMM )
calculation 10001, that is 16 + 1 = 17

Finally tell you I found a very cow b things

int a;
printf("%x",&a);

This thing can be output to a hexadecimal form, amazing, do question yesterday when stuck, and do not consider the problem leading 0, trained, trained, trained, or not enough basis for
a specific lot, see Gangster summary:
https://blog.csdn.net/taxue4485/article/details/40684703?utm_source=app

Published 32 original articles · won praise 12 · views 1396

Guess you like

Origin blog.csdn.net/qq_18873031/article/details/98471298