zoukankan      html  css  js  c++  java
  • 轻松实现C/C++各种常见进制相互转换

    其它进制转为十进制

    在实现这个需求之前,先简单介绍一个c标准库中的一个函数:

    long strtol( const char *str, char **str_end, int base);
    

    参数详细说明请参考文档

    注意:这个函数在c标准库stdlib中,所以需要#include<cstdlib>

    用法参考

    #include <stdio.h>
    #include <errno.h>
    #include <stdlib.h>
     
    int main(void)
    {
        // parsing with error handling
        const char *p = "10 200000000000000000000000000000 30 -40 junk";
        printf("Parsing '%s':
    ", p);
        char *end;
        for (long i = strtol(p, &end, 10);p != end;i = strtol(p, &end, 10))
        {
            printf("'%.*s' -> ", (int)(end-p), p);
            p = end;
            if (errno == ERANGE){
                printf("range error, got ");
                errno = 0;
            }
            printf("%ld
    ", i);
        }
     
        // parsing without error handling
        printf(""1010" in binary  --> %ld
    ", strtol("1010",NULL,2));
        printf(""12" in octal     --> %ld
    ", strtol("12",NULL,8));
        printf(""A"  in hex       --> %ld
    ", strtol("A",NULL,16));
        printf(""junk" in base-36 --> %ld
    ", strtol("junk",NULL,36));
        printf(""012" in auto-detected base  --> %ld
    ", strtol("012",NULL,0));
        printf(""0xA" in auto-detected base  --> %ld
    ", strtol("0xA",NULL,0));
        printf(""junk" in auto-detected base -->  %ld
    ", strtol("junk",NULL,0));
    }
    

    Output

    Parsing '10 200000000000000000000000000000 30 -40 junk':
    '10' -> 10
    ' 200000000000000000000000000000' -> range error, got 9223372036854775807
    ' 30' -> 30
    ' -40' -> -40
    "1010" in binary  --> 10
    "12" in octal     --> 10
    "A"  in hex       --> 10
    "junk" in base-36 --> 926192
    "012" in auto-detected base  --> 10
    "0xA" in auto-detected base  --> 10
    "junk" in auto-detected base -->  0
    

    更多详细说明请参考文档

    接下来使用这个函数来实现其它进制转为十进制的需求,具体请参考代码:

    #include<iostream>
    #include<cstdlib>
    using namespace std;
    int main(){
        //把8进制的17转化为10进制打印输出
     	string str = "17";
     	char *tmp ;
     	long result = strtol(str.c_str(),&tmp,8);
     	cout<<result;
    	return 0;
    }
    

    Output

    15
    

    十进制转为其他进制

    目前没有找到可以使用的库函数来方便的实现这个需求,所以自己实现了一下,具体请参考代码:

    #include<iostream>
    #include<algorithm>
    using namespace std;
    //digital为10进制数,r为需要转换的目标进制,返回目标进制数
    string dtox(int digital,int r){
    	string result="";
    	const char s[37]="0123456789abcdefghijklmnopqrstuvwxyz";
    	if(digital==0){
    		return "0";
    	}
    	while(digital!=0){
    		int tmp =digital%r;
    		result+=s[tmp];
    		digital/=r;
    	}
    	reverse(result.begin(),result.end());
    	return result;
    }
    int main(){
    	cout<<"十进制10转为16进制结果:"<<dtox(10,16)<<endl;
    	cout<<"十进制10转为8进制结果:"<<dtox(10,8)<<endl;
    	cout<<"十进制10转为2进制结果:"<<dtox(10,2)<<endl;
    	cout<<"十进制10转为10进制结果:"<<dtox(10,10)<<endl;
    }
    

    Output:

    十进制10转为16进制结果:a
    十进制10转为8进制结果:12
    十进制10转为2进制结果:1010
    十进制10转为10进制结果:10
    

    实现效果还算理想,另外,这个函数还可以把10进制数转化为不常用的其他进制,不局限于2,8,10,16等常见进制。但是r的有效范围应该为2-36。

    另外,函数并没有考虑负数以及浮点数,r不合法的情况

  • 相关阅读:
    社交类app开发( 仿陌陌 客户端+服务器端)
    iPhone的xib与iPad的xib相互转换
    SVN的搭建
    使用企业证书给iOS应用重签
    [破解版]Unity3d引擎最新稳定版本4.5.5下载(官方最新稳定版本)
    iphone开发资源汇总
    iOS绘图教程
    iOS静态库相关-封装lib
    iOS内存管理策略和实践
    前台中文乱码
  • 原文地址:https://www.cnblogs.com/ericling/p/11813387.html
Copyright © 2011-2022 走看看