C++任意数字类型转 2进制、8进制、16进制

C++任意数字类型转 2进制、8进制、16进制

平时我们在写程序的过程中会经常碰见进制转换的操作,偶尔写一次还好每次写,我们都又要重新定义函数进行转换;在这里博主就分享一下我自己编写的一个进制转换的方法吧,也比较通用;

(如没有耐心可以直接跳过思路 看尾部的源代码)
先说说我的思路(这里折中介绍转8进制的思路,转16进制大同小异)
1、将整数转换成二进制格式 以字符串的形式存放;在这里插入图片描述
博主在这里使用的是模板函数,这样可以提高函数的兼容性,然后呢在这里转换成二进制 的方法我采用的是位运算来进行的操作,这样的好处是极大的提高了程序的效率
稍微的这里解释一下这种算法的逻辑:
举个例子比如我们传进来的参数是10
我们要将10 转换成二进制格式;
10 在计算机的内存地址本身就是以二进制的方式存放的
10(binary 1010)
这个二进制数的最高位是 第4位
1 << 3 的意思呢就是 将1往左移3位 0001(1) => 1000(8)
10 & (1<< 3) => 1010 & 1000
&运算符呢是位运算符里的按位与 什么意思呢就是两个二进制数 相同位上如果都为1
那么这个表达式的值为1 否则为0
10 & (1<< 3) => 1010 & 1000 = 1
10 & (1<< 2) => 1010 & 100 = 0
10 & (1<< 1) => 1010 & 10 = 1
10 & (1<< 0) => 1010 & 1 = 0
好这样就得到了 10 十进制数的二进制格式 1010

2、检索二进制长度是否为3的倍数如果不是前面补0;
在这里插入图片描述
这一步操作就是用来补“0” 的为什么要这么做请看下一步的的讲解

3、将这个字符串进行拆分(3个为一组)例如“010” 我们就把它看作成 2
在这里插入图片描述
我们之所以要把二进制字符串补成3的倍数,就是因为我们把这个二进制字符的子串长度为3分成一组 例如 ”001010100“ =》 ”001“ ”010“ ”100“
如图我我们采用的if else if 来进行字符串的(switch 只能比较 数字类型的 )
下面理下这一步操作的过程:
例子 1259传进来 转换城二进制 =》 10011101011
补零 =》 010011101011
拆分 =》 010 011 101 011
比对 =》 --2----3----5----3

4、定义字符串变量s_oct 一直合并迭代上面的结果;
这一步的操作就简单了 就是 定义一个字符串 s_oct = ”\0“;
让后把上面一步得到的 --2----3----5----3
每个数字以字符串的形式叠加起来:
s_oct + 2 + 3 + 5 + 3 = 2353

5、最后输出s_oct;
所以1259的八进制形式就是2353;
return s_oct;

整个案例的源代码

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";
}
发布了27 篇原创文章 · 获赞 62 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42359956/article/details/87625522