zoukankan      html  css  js  c++  java
  • C++中的各种进制转换函数汇总

    C++中的各种进制转换函数汇总

    1.在C中,按指定进制格式输出如下:

    #include <iostream>
    #include <cstdio>
    using namespace std;
    int main()
    {                                         
        printf("%o\n",35);    //  八进制格式输出:%o
        printf("%d\n",35);    //  十进制格式输出:%d
        printf("%x",35);      //  十六进格式制输出:%x或者%X
        return 0;
    }
    //  输出结果:
    43
    35
    23
    

    2.在C++中,按指定进制格式输出如下:

    #include <iostream>
    using namespace std;
    int main()
    {                                         
        cout<<oct<<35<<endl;    //  八进制输出格式   oct
        cout<<dec<<35<<endl;    //  十进制输出格式   dec
        cout<<hex<<35<<endl;    //  十六进制输出格式 hex
        return 0;
    }
    //  输出:
    43
    35
    23
    

    3.任意2-36进制数转化为10进制数

    1. 自己写一个函数

    建议自己可以去敲一敲,加深记忆

    #include <iostream>
    using namespace std;
    int turn (string a,int t)
    {
        int sum=0;
        for(int i=a.size()-1;i>=0;i--)
        {
            if(a[i]>='0'&&a[i]<='9')
                sum=sum*t+a[i]-'0';
            else
            {
                sum=sum*t+a[i]-'A'+10;
            }        
        }
        return sum;
    }
    int main()
    {                                         
        string a;
        int num,t;
        while(cin>>a)   //  超过10进制我们以大写字母表示
        {
            cin>>t;    //  t代表要转换的进制数
            num=turn(a,t);
            cout<<num<<endl;
        }
        return 0;
    }
    

    2.strtol函数

    long int strtol(const char nptr, char **endptr, int base)
    strtol()会将nptr指向的字符串,根据参数base,按权转化为long int, 然后返回这个值。
    参数base的范围为2~36,和0;它决定了字符串以被转换为整数的权值。
    可以被转换的合法字符依据base而定,举例来说,当base为2时,合法字符为‘0’,‘1’;base为8时,合法字符为‘0’,‘1’,……‘7’;base为10时,合法字符为‘0’,‘1’,……‘9’;base 为16时,合法字符为‘0’,‘1’,……‘9’,‘a’,……‘f’;base为24时,合法字符为‘0’,……‘9’,‘a’,……‘n’,base为36时,合法字符为‘0’,……‘9’,‘a’,……‘z’;等等。其中,不区分大小写,比如,‘A’和‘a’会都会被转化为10。
    当字符合法时,‘0’,……‘9’依次被转换为十进制的0~9,‘a’,……‘z’一次北转换为十进制的10~35。
    strtol()函数检测到第一个非法字符时,立即停止检测,其后的所有字符都会被当作非法字符处理。合法字符串会被转换为long int, 作为函数的返回值。非法字符串,即从第一个非法字符的地址,被赋给
    endptr。**endptr是个双重指针,即指针的指针。strtol()函数就是通过它改变*endptr的值,即把第一个非法字符的地址传给endptr。多数情况下,endptr设置为NULL, 即不返回非法字符串。

    #include <iostream>
    #include <cstring>
    using namespace std;
    int main()
    {
        char r[100];
        int t;
        while(~scanf("%s",r))
        {
            cin>>t;    //  t代表他原来的进制数
            cout<<strtol(r,NULL,t)<<endl;
            memset(r,'\0',sizeof(r));
        }
        return 0;
    }
    //  C++11 特性!!!
    

    注意:

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

    4.将10进制数转换为任意的n进制数

    • 建议自己写一个代码:

      #include <iostream>
      #include <cstring>
      #include <stack>
      using namespace std;
      stack<char> s;
      void turn (int t,int tmp)
      {
          while(t!=0)
          {
              if(t%tmp<=10)
              s.push(t%tmp+'0');
              else
              s.push(t%tmp-10+'A');
              t/=tmp;
          }
      }
      int main()
      {
          int t,tmp;
          while(cin>>t>>tmp)
          {
              turn(t,tmp);
              while(!s.empty())
              {
                  cout<<s.top();
                  s.pop();
              }
              cout<<endl;
          }
          return 0;
      }
      
    • itoa函数

      函数原型:*char**itoa(``int value,``char``*string,``int radix);

      例如:itoa(num, str, 2); num是一个int型的,是要转化的10进制数,str是转化结果,后面的值为目标进制。

      PSitoa并不是一个标准的函数,而是一个windows所特有的,如需要跨平台请使用sprintf

      #include<cstdio> 
      #include<cstdlib>  //  引入的头文件
      int main()  
      {  
          int num = 10;  
          char str[100];  
          itoa(num, str, 2);  //c++中一般用_itoa,用itoa也行,
          printf("%s\n", str);  
          return 0;  
      }
      

      三)使用字符串流stringstream

      引入头文件#include
      1.将八,十六进制转十进制。
      #include<iostream>
      #include<string>
      #include<sstream>
      using namespace std;
      int main(void)
      {
      	string s="20";
      	int a;
      	stringstream ss;
      	ss<<hex<<s;    //以16进制读入流中
      	ss>>a;        //10进制int型输出
      	cout<<a<<endl;
              return 0;
      }
      //输出:32
      

      2.将十进制转八,十六进制。

      #include<cstdio>
      #include<iostream>
      #include<string>
      #include<sstream>
      using namespace std;
      int main(void)
      {
      	string s1,s2;
      	int a=30;
      	stringstream ss;
      	ss<<oct<<a;        //10进制转成八进制读入流中,再以字符串输出
      	ss>>s1;			
      	cout<<s1<<endl;        //输出:36
      	ss.clear();		//不清空可能会出错。
      	ss<<hex<<a;		 //10进制转成十六进制读入流中,,再以字符串输出
      	ss>>s2;			
      	cout<<s2<<endl;        //输出:1e
          return 0;
      }
      
  • 相关阅读:
    C# winform窗体间传值(使用委托或事件)
    C# ListView用法详解
    C# ListView列表包含添加和删除,自动排序
    C#跨窗体传值的几种方法分析(很详细)
    c#listview控件的数据添加和常用事件的处理
    C#中结构体与字节流互相转换 [StructLayout(LayoutKind.Sequential)]
    C# winform 操作access常用类
    发行自己的区块链加密货币
    以太坊自助发币
    supervisor常用命令
  • 原文地址:https://www.cnblogs.com/Yqifei/p/12660076.html
Copyright © 2011-2022 走看看