C++数制转换

十进制转换成n进制

这里n的范围是(1,9],将十进制转化成n进制的思路就是 循环求模、相除
实现代码如下

//将十进制 dec 转换成 n 进制,结果存在 other中,other在外部分配内存
void dec2other(int dec, char *other, int n)
{
    assert(n>1 && n<= 9);
    int i=0;
    char* tmpArray = new char[64];
    while(dec != 0){
        tmpArray[i] = dec%n+'0';//int转char
        dec /= n;
        i++;
    }

    //n进制的串的高位 保存在 tmpArray的高位,
    //如 dec=6,二进制为110,此时保存的tmpArray[0~2] = {0,1,1},逆序
    int len = i; //n进制串长
    for(i=0;i<len;i++){ //下面将tmpArray颠倒,存在other中
        other[i] = tmpArray[len-i-1];
     } //other[0~2]= {1,1,0} 这就是结果
    other[len] = '/0';
    delete[] tmpArray;
}

输入十进制数dec,要求转化成n进制的数并存放在other字符数组中。首先判断使用assert宏,判断n的合法性。然后new一个临时char数组,用于保存转换过程中的值。执行while循环,只要dec不为0,则将dec对n求模,并将结果转换为char型保存在tmpArray中。循环结束,tmpArray中保存了转换后的结果,但为逆序。最后将tmpArray逆序赋值给other,得到结果。调用示例如下

int dec = 128;
char* bin = new char[64];
dec2other(dec,bin,2);//转化成二进制
CString szNumber;
szNumber.Format("%s",bin);
AfxMessageBox(szNumber);//输出:10000000

char* octal = new char[64];
dec2other(dec,octal,8);//转化成八进制
szNumber.Format("%s",octal);
AfxMessageBox(szNumber);//输出:200

十进制转换成十六进制

基本思路和上面的一样,区别在于十六进制多出了'A'、'B'、'C'、'D'、'E'、'F' 六个字符。当循环求模时,得到的结果大于9时,就要转换成相应的字母表示。代码如下

//hex 在外部分配空间
void dec2hex(int dec, char *hex)
{
    int i=0,flag;
    char* tmpArray = new char[64];
    while(dec != 0){
        flag = dec%16;
        //此次也可不使用switch-case,直接把字符保存在数组中,通过数组赋值实现
        switch(flag){
        case 10:
            tmpArray[i] = 'A';
            break;
        case 11:
            tmpArray[i] = 'B';
            break;
        case 12:
            tmpArray[i] = 'C';
            break;
        case 13:
            tmpArray[i] = 'D';
            break;
        case 14:
            tmpArray[i] = 'E';
            break;
        case 15:
            tmpArray[i] = 'F';
            break;
        default:
            tmpArray[i] = flag+'0';
            break;
        }
        dec /= 16;
        i++;
    }
    int len = i; //n进制串长
    for(i=0;i<len;i++){
        hex[i] = tmpArray[len-i-1];
    }
    hex[len] = '/0';
    delete[] tmpArray;
}

二进制转换成十进制

思路很简单,就是把二进制串的每一位上的数,乘上对应位上的权值,累加起来。

比如,二进制"110",对应的十进制为12^2 + 12^1+02^0 = 14+12+01 = 6。(^表示幂)

实现代码如下

//二进制串保存在bin[]中,串长为n,函数返回对应的十进制数
int bin2dec(char bin[],int len){
    int dec = 0;//保存十进制结果
    for(int i=0;i<len;i++){
        if(bin[i] == '1'){
            dec += (int)pow(2,len-i-1); //计算 2 的 len-i 次方,并累加到最终结果
        }
        else if(bin[i] == '0'){
        }
        else{
            //输入二进制串有误!
            return 0;
        }
    }
    return dec;
}

八进制转十进制

思路与二进制转十进制相同。代码如下

//八进制串保存在oct[]中,串长为len,函数返回对应的十进制数
int oct2dec(char oct[],int len){
    int dec = 0;//保存十进制结果

    int nOct;
    for(int i=0;i<len;i++){
        if(oct[i] == '0'){ nOct = 0;}
        else if(oct[i] == '1'){ nOct = 1;}
        else if(oct[i] == '2'){ nOct = 2;}
        else if(oct[i] == '3'){ nOct = 3;}
        else if(oct[i] == '4'){ nOct = 4;}
        else if(oct[i] == '5'){ nOct = 5;}
        else if(oct[i] == '6'){ nOct = 6;}
        else if(oct[i] == '7'){ nOct = 7;}
        else{
            //输入八进制串有误!
            return 0;
        }
        dec += (int)(nOct*pow(8,len-i-1));
    }
    return dec;
}

十六进制转十进制

思路与二进制、八进制转十进制相同。代码如下

//十六进制串保存在hex[]中,串长为len,函数返回对应的十进制数
    int hex2dec(char hex[],int len){
    int result = 0;//保存转换的十进制数结果

    int nHex;
    for(int i=0;i<len;i++){
        if(hex[i] == '0'){ nHex = 0;}
        else if(hex[i] == '1'){ nHex = 1;}
        else if(hex[i] == '2'){ nHex = 2;}
        else if(hex[i] == '3'){ nHex = 3;}
        else if(hex[i] == '4'){ nHex = 4;}
        else if(hex[i] == '5'){ nHex = 5;}
        else if(hex[i] == '6'){ nHex = 6;}
        else if(hex[i] == '7'){ nHex = 7;}
        else if(hex[i] == '8'){ nHex = 8;}
        else if(hex[i] == '9'){ nHex = 9;}
        else if(hex[i] == 'A'){ nHex = 10;}
        else if(hex[i] == 'B'){ nHex = 11;}
        else if(hex[i] == 'C'){ nHex = 12;}
        else if(hex[i] == 'D'){ nHex = 13;}
        else if(hex[i] == 'E'){ nHex = 14;}
        else if(hex[i] == 'F'){ nHex = 15;}
        else{
          //输入十六进制串有误!
          return 0;
        }
    dec += (int)(nHex*pow(16,len-i-1));
    }
    return dec;
}

利用栈实现十进制转换为n进制

//《数据结构 c语言版》(严蔚敏) P48
void conversion(int N){
    InistStack(S);
    scanf("%d",dec); //从屏幕读取一个十进制
    while(dec != 0){ //转换
        Push(S,dec%N);
        dec /= N;
    }
    while(!StackEmpty(S)){ //显示结果
        Pop(S,e);
        printf("%d",e);
    }
}

PS:上述代码严格的说存在许多问题,比如没有考虑负数,代码不够简洁。后续有机会再改。另外,基于上述代码,我用MFC实现了一个MFC进制转换小工具,可是实现上述各种数制实时转换。软件可点此下载(貌似需要1积分,我没找到怎么修改,算了,如果需要,请留邮箱吧)。如需要源码,也请留邮箱。

数制转换软件

——————————————————————

该文2010-11-28 19:32首发于我的CSDN专栏。有改动。

作者:JarvisChu
原文链接:C++数制转换
版权声明:自由转载-非商用-非衍生-保持署名 | Creative Commons BY-NC-ND 3.0

1 条评论

发表评论

取消回复