• C++中各种常用转换汇总学习


    进制转换

    指定格式输出

    printf

    printf("%05o
    ",32);    //八进制格式,保留5位,高位补零
    printf("%03d
    ",32);    //十进制格式,保留3位,高位补零
    printf("%05x
    ",32);    //十六进制格式,保留5位,高位补零
    

    cout

    cout << dec << 32 << endl; //十进制,dec可以省略
    cout << hex << 32 << endl; //十六进制
    cout << oct << 32 << endl; //八进制
    cout << bitset<8> (32) << endl; //二进制,保留八位,需要头文件<bitset>,详细在下面bitset中
    

    手写进制转换

    手写各种花式转换不再赘述,这里主要提一下二进制、八进制、十六进制相互转换的技巧,8 = 2 ^ 3, 16 = 2 ^ 4

    二进制转八进制

    所以二进制转换成八进制的时候,只需要将二进制的表示从右往左开始,每三位二进制数为1组 ,分到最后如果不足3位,那么剩下多少位就是多少位,再用每组的二进制的每一位数从右往左依次乘以20、21、2^2然后相加,得出一组的结果,最后将所有组的结果相连(不是相加)

    二进制转换为十六进制

    二进制转换为十六进制就是将二进制每四位二进制为一组,其他与八进制转换为二进制一样。

    八进制转换为二进制

    只需要将八进制的每一个数用三位二进制表示,然后相连既可以。

    十六进制转换为二进制

    只要需要将十六进制的每一个数用四位二进制表示,然后相连即可。

    八进制和十六进制相互转换

    以二进制作为中转

    可以用于进制转换的字符串流和函数

    sprintf

    可以将一个10进制数转换为指定格式的n进制字符串
    优点:可以转换各种进制的数。
    缺点:需要先分配足够的char数组。
    函数原型

    int sprintf( char *buffer, const char *format, [ argument] … );
    

    原型说明:
    buffer:char型指针,指向将要写入的字符串的缓冲区。
    format:格式化字符串。(格式控制)
    [argument]...:可选参数,可以是任何类型的数据。
    返回值:字符串长度(strlen)
    如:

    #include <cstdio>  
    #include <iostream>
    #include <string.h>
    using namespace std;
     
    int main() {  
     int aa = 30;
     char c[8];
     int length = sprintf(c, "%05X", aa);
     cout << c << endl; // 0001E
     return 0;
    }
    

    还有网上copy的一份应用:

    #include<cstdio>  
    int main()  {  
    	char s[100]={0};
    	sprintf(s, "%d", 123); //十进制输出产生"123"
    	sprintf(s, "%4d%4d", 123, 4567); //指定宽度不足的左边补空格,产生:" 1234567"
    	sprintf(s, "%8o", 123);	//八进制输出,宽度占8个位置
    	sprintf(s, "%8x", 4567); //小写16 进制,宽度占8 个位置,右对齐
    	sprintf(s, "%10.3f", 3.1415626); //产生:" 3.142"
    	int i = 100;
    	sprintf(s, "%.2f", i);	//注意这是不对的
    	sprintf(s, "%.2f", (double)i);	//要按照这种方式才行
    	return 0;  
    }  
    

    stringstream

    需要头文件#include
    缺点:慢

    十进制转八、十六进制

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <sstream>
    using namespace std;
    
    int main() {
          string s, s2;
          long long x;
          stringstream ss;
          cin >> s;
          ss << hex << s;
          ss >> x;
          cout << x << endl;
          return 0;
    }
    

    八、十六进制转十进制

    #include <iostream>
    #include <string.h>
    #include <sstream>
    using namespace std;
    
    int main() {
          string s = "11";
          int a;
          stringstream ss;
          ss << hex << s;    //以16进制读入流中
          ss >> a;           //10进制int型输出
          cout << a << endl;
          return 0;
    }
    //
    

    十六、八进制相互转换

    如果数字不大的话可以以十进制作为中转,超long long 的见手打十六、八、二进制的相互转换

    strtol

    string to long
    函数原型

    long int strtol(const char *nptr, char **endptr, int base)
    

    原型说明:
    base是要转化的数的进制
    非法字符会赋值给endptr
    nptr是要转化的字符
    如:

    #include <cstdio>
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    using namespace std;
     
    int main() {  
        string s = "10549stend#12";  
        char *stop;  
        int ans = strtol(s.c_str(), &stop, 8);  
        cout << oct << ans << endl;
        cout << ans << endl;
        cout << stop << endl; 
        return 0;
    }
    

    Tips:
    [1]如果base为0,且字符串不是以0x(或者0X)开头,则按十进制进行转化。
    [2]如果base为0或者16,并且字符串以0x(或者0X)开头,那么,x(或者X)被忽略,字符串按16进制转化。
    [3]如果base不等于0和16,并且字符串以0x(或者0X)开头,那么x被视为非法字符。
    [4]对于nptr指向的字符串,其开头和结尾处的空格被忽视,字符串中间的空格被视为非法字符。

    itoa

    可以将一个10进制数转换为任意的2-36进制字符串(即可以完成进制转换和int转string两个功能)
    重要的注意要先说:itoa并不是一个标准的C/C++函数,它是Windows特有的
    函数原型

    char *itoa( int value, char *string,int radix);
    

    原型说明:
    value:欲转换的数据。
    string:目标字符串的地址。
    radix:转换后的进制数
    返回指向string这个字符串的指针
    如:

    #include <cstdio> 
    #include <iostream>
    #include <cstdlib> 
    #include <string.h>
    using namespace std;
    int main() {  
        int num = 10;  
        string s; 
        char str[110];
        _itoa(num, str, 2);  //c++中一般用_itoa,用itoa也行,
        s = str;
        cout << s << endl;
        return 0;  
    }
    

    bitset

    需包含头文件 #include

    构造函数

    如(转自https://www.cnblogs.com/magisk/p/8809922.html)

        bitset<4> bitset1;  //无参构造,长度为4,默认每一位为0
    
        bitset<8> bitset2(12);  //长度为8,二进制保存,前面用0补充
    
        string s = "100101";      //只能是01串
        bitset<10> bitset3(s);  //长度为10,前面用0补充
        
        char s2[] = "10101";
        bitset<13> bitset4(s2);  //长度为13,前面用0补充
    
        bitset<2> bitset5(12);  //12的二进制为1100(长度为4),但bitset1的size=2,只取后面部分,即00
    
        cout << bitset1 << endl;  //0000
        cout << bitset2 << endl;  //00001100
        cout << bitset3 << endl;  //0000100101
        cout << bitset4 << endl;  //0000000010101
        cout << bitset5 << endl;   //00
    

    支持下标访问,最低为下标为0

    常用函数

    .count() 用来求有几个1
    .size() 求大小(位数)
    .test(i) 判断下标为i的的元素是0还是1,1返回true, 0返回false
    .any() 判断是否含1
    .none() 判断是否不含1
    .all() 判断是否都是1
    如(转自https://www.cnblogs.com/magisk/p/8809922.html):

        bitset<8> foo ("10011011");
    
        cout << foo.count() << endl;  //5  (count函数用来求bitset中1的位数,foo中共有5个1
        cout << foo.size() << endl;   //8  (size函数用来求bitset的大小,一共有8位
    
        cout << foo.test(0) << endl;  //true  (test函数用来查下标处的元素是0还是1,并返回false或true,此处foo[0]为1,返回true
        cout << foo.test(2) << endl;  //false  (同理,foo[2]为0,返回false
    
        cout << foo.any() << endl;  //true  (any函数检查bitset中是否有1
        cout << foo.none() << endl;  //false  (none函数检查bitset中是否没有1
        cout << foo.all() << endl;  //false  (all函数检查bitset中是全部为1
    

    int string 相互转换

    int 转 string

    itoa/stringstream/sprintf 见上

    string 转 int

    strtol/stringstream 见上

    sscanf

    详见百科:https://baike.baidu.com/item/sscanf/10551550?fr=aladdin

    大小写转换

    char转换 toupper, tolower

    transform

    原型

    template < class InputIterator, class OutputIterator,
     class UnaryOperator >  
          OutputIterator transform ( InputIterator first1, InputIterator last1,  
                                     OutputIterator result, UnaryOperator op );  
          
        template < class InputIterator1, class InputIterator2,  
                   class OutputIterator, class BinaryOperator >  
          OutputIterator transform ( InputIterator1 first1, InputIterator1 last1,  
                                     InputIterator2 first2, OutputIterator result,  
                                     BinaryOperator binary_op );
    

    如(转自https://www.cnblogs.com/balingybj/p/4678850.html)

    #include <string>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
            string strA = "yasaken@126.com";
            string strB = "LURY@LENOVO.com";
            printf("Before transform:
    ");
            printf("strA:%s 
    ", strA.c_str());
            printf("strB:%s 
    
    ", strB.c_str());
    
            transform(strA.begin(), strA.end(), strA.begin(), ::toupper);
            transform(strB.begin(), strB.end(), strB.begin(), ::toupper);
            printf("After transform to toupper:
    ");
            printf("strA:%s 
    ", strA.c_str());
            printf("strB:%s 
    
    ", strB.c_str());
    
            transform(strA.begin(), strA.end(), strA.begin(), ::tolower);
            transform(strB.begin(), strB.end(), strB.begin(), ::tolower);
            printf("After transform to lower:
    ");
            printf("strA:%s 
    ", strA.c_str());
            printf("strB:%s 
    
    ", strB.c_str());
            return 0;
    }
  • 相关阅读:
    Git 游离态的一次问题解决
    idea每次新建项目的默认路径
    springboot 整合 freemarker
    Linux 学习网站
    springtask 基本使用和 cron 表达式
    volatile 关键字 和 i++ 原子性
    python 自动补全
    nagios维护之常见问题
    nagios维护之添加监控
    windows下python文件与文件夹操作
  • 原文地址:https://www.cnblogs.com/Eirlys/p/13736920.html
Copyright © 2020-2023  润新知