C ++ type conversion of any digital binary, octal, hexadecimal

C ++ type conversion of any digital binary, octal, hexadecimal

Usually we often met in the process of writing a program operating base for the conversion, and occasionally write once each write Fortunately, we all have to redefine the function to convert; here it is bloggers share what I have written a hex method of converting it, is relatively common;

(E.g., patient can not see the source code Thinking skip tail)
first talk my thoughts (described here compromises the idea of transfer octal, hexadecimal similar transfer)
1, converted to a binary integer format string in the form of storage;Here Insert Picture Description
bloggers here are using a template function, this can increase the compatibility functions, and where it converted into binary method I used is the operating bit operation carried out, so that the benefits are greatly improved the efficiency of the program
is slightly herein explain the logic of this algorithm:
for example parameters passed in our example is 10
we want to convert into binary format 10;
10 is stored in binary fashion in the computer's own memory address
10 (binary 1010)
the most significant bit of the binary number is 4
1 << 3, this means is that the 1 to the left three 0001 (1) => 1000 (8)
10 & (1 << 3) => 1010 & 1000
& operator it is based on what is the position of operators in the same position and what is the meaning of two-bit binary number is 1 if
then the expression evaluates to 1 or 0
10 & (1 << 3) = > 1010 & 1000 = 1
10 & (1 << 2) => 0 = 1010 & 100
10 & (1 << 1) => 1 = 1010 & 10
10 & (1 << 0) => 1010 &

2, retrieve binary length is a multiple of 3, if not a leading zero;
Here Insert Picture Description
this step is used to fill a "0" Why do you do see the next step is to explain

3, the string will be split (three as a group) such as "010" We'll take it as into a 2
Here Insert Picture Description
reason why we should complement binary string into a multiple of 3, this is because we put the sub binary characters a run length of 3 is divided into groups such as "001010100" = "" 001 "" 010 "" 100 "
to the character string shown I we use the if else if (switch types can compare digital)
processing at this step following during operation:
examples 1259 passed in binary conversion city = '10011101011
zero-padded = "010 011 101 011
split =" 010011101011
match = "--2. 3 ---- ---- ----. 3. 5

4, define a string variable s_oct been consolidated results of the iteration above;
this step of the operation is simple is the definition of a string s_oct = "\ 0";
After allowing the step to get above --2 ---- 3 - --5. 3 ----
each digital string of superimposed together:
s_oct. 3 + 2 + + +. 5. 3 = 2353

5, the final output s_oct;
octal form so 1259 is 2353;
return s_oct;

The source code for the entire case :

template<typename T>
string to_binary(T num,int n){
    string s_bin="\0",s_oct="\0",s_hex="\0";
    int i = sizeof(T)*8-1;
    while(!(num & (1<<i))){
        i--;
    }
    for(int j=i;j>=0;j--){
        if(num & (1<<j)){
            s_bin += "1";
        }
        else{
            s_bin += "0";
        }
    }
    if(n == 2){
        return s_bin;
    }
    if(n == 8){
        while(s_bin.length()%3!=0){
            s_bin = "0"+s_bin;
        }
        for(int j=0;j<(int)s_bin.length();j+=3){
            string sub_str = s_bin.substr(j,3);// 注意substr的第一个参数代表起始点 第二个参数代表偏移量
            if(sub_str == "000"){s_oct += "0";}
            else if(sub_str == "001"){s_oct += "1";}
            else if(sub_str == "010"){s_oct += "2";}
            else if(sub_str == "011"){s_oct += "3";}
            else if(sub_str == "100"){s_oct += "4";}
            else if(sub_str == "101"){s_oct += "5";}
            else if(sub_str == "110"){s_oct += "6";}
            else if(sub_str == "111"){s_oct += "7";}
        }
       return s_oct;
    }
    if(n == 16){
        while(s_bin.length()%4!=0){
            s_bin = "0"+s_bin;
        }
        for(int j=0;j<(int)s_bin.length();j+=4){
            string sub_str = s_bin.substr(j,4);
            if(sub_str == "0000"){s_hex += "0";}
            else if(sub_str == "0001"){s_hex += "1";}
            else if(sub_str == "0010"){s_hex += "2";}
            else if(sub_str == "0011"){s_hex += "3";}
            else if(sub_str == "0100"){s_hex += "4";}
            else if(sub_str == "0101"){s_hex += "5";}
            else if(sub_str == "0110"){s_hex += "6";}
            else if(sub_str == "0111"){s_hex += "7";}
            else if(sub_str == "1000"){s_hex += "8";}
            else if(sub_str == "1001"){s_hex += "9";}
            else if(sub_str == "1010"){s_hex += "A";}
            else if(sub_str == "1011"){s_hex += "B";}
            else if(sub_str == "1100"){s_hex += "C";}
            else if(sub_str == "1101"){s_hex += "D";}
            else if(sub_str == "1110"){s_hex += "E";}
            else if(sub_str == "1111"){s_hex += "F";}
        }
        return s_hex;
    }
    return "EOF";
}
Published 27 original articles · won praise 62 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42359956/article/details/87625522