您的位置:首页 > 编程语言 > C语言/C++

将数字转为二、十、十六进制字符串

2017-03-14 10:47 337 查看
MSDN的例子

Example

指定要转换的进制的基数,其值好象在1--36之间都可以

这个不是C标准库中的函数,而是Windows平台下扩展的,标准库中有sprintf,功能比这个更强,用法跟printf类似:

char str[255];

sprintf(str, "%x", 100); //将100转为16进制表示的字符串。

atoi() 函数用来将字符串转换成整数(int),其原型为:

int atoi (const char * str);

【函数说明】atoi() 函数会扫描参数 str 字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过 isspace() 函数来检测),直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。

【返回值】返回转换后的整型数;如果 str 不能转换成 int 或者 str 为空字符串,那么将返回 0。

温馨提示:ANSI C 规范定义了 stof()atoi()atol()strtod()strtol()strtoul() 共6个可以将字符串转换为数字的函数,大家可以对比学习。另外在
C99 / C++11 规范中又新增了5个函数,分别是 atoll()、strtof()、strtold()、strtoll()、strtoull(),在此不做介绍,请大家自行学习。

范例:将字符串a 与字符串b 转换成数字后相加。

#include <stdio.h>
#include <stdlib.h>

int main ()
{
    int i;
    char buffer[256];
    printf ("Enter a number: ");
    fgets (buffer, 256, stdin);
    i = atoi (buffer);
    printf ("The value entered is %d.", i);

    system("pause");
    return 0;
}

执行结果:

Enter a number: 233cyuyan

The value entered is 233.

一、C++的int转string
 #方法一: 使用itoa函数:    char *  itoa ( int value, char * str, int base );                            
说明:Convert integer to string (non-standard function)
参数:
·value : Value to be converted to a string.·str : Array in memory where to store the resulting null-terminated string.·base : Numerical base used to represent the value as a string, between2 and36, where10 means decimal base, 16 hexadecimal,8 octal, and2 binary.
Demo:

[cpp] view
plain copy

#include <iostream>   

using namespace std;   

int main(int argc, char* argv[])    

{      

    int n = 30;   

    char c[10];     

    //二进制转换  

    itoa(n, c, 2);    

    cout << "2-> " << c << endl;   

    //十进制转换  

    itoa(n, c, 10);      

    cout << "10-> " <<  c << endl;  

    //十六进制转换  

    itoa(n, c, 16);       

    cout << "16-> " <<  c << endl;   

  

    system("pause");       

    return 0;    

}  

输出结果:

2-> 11110

10-> 30

16-> 1e

请按任意键继续. . .

  #方法二: 使用sprintf:   int sprintf ( char * str, const char * format, ... );

参数说明:

% 印出百分比符号,不转换。

b 整数转成二进位。

c 整数转成对应的 ASCII 字元。

d 整数转成十进位。

f 倍精确度数字转成浮点数。

o 整数转成八进位。

s 整数转成字串。

x 整数转成小写十六进位。

X 整数转成大写十六进位。

Demo:

[cpp] view
plain copy

#include <iostream>  

#include <string>  

using namespace std;    

int main()   

{       

    int n = 30;   

    char c[20];     //char *c;  

    //%d十进制  

    sprintf(c, "%d", n);     

    cout << c << endl;      

    //%o八进制  

    sprintf(c, "%o", n);     

    cout << c << endl;     

    //%X大写十六进制  

    sprintf(c, "%X", n);     

    cout << c << endl;    

    //%cACSII字元  

    sprintf(c, "%c", n);     

    cout << c << endl;    

      

    //%f浮点数转换  

    float f = 24.678;      

    sprintf(c, "%f", f);   

    cout << c << endl;     

    //%.2f"保留小数点后面两位  

    sprintf(c, "%.2f", f);      

    cout << c << endl;    

    //转换两个数  

    sprintf(c, "%d-%.2f", n, f);    

    cout << c << endl;    

  

    system("pause");      

    return 0;    

}    

输出结果:


30

36

1E

//注:这里是个特殊符号

24.677999

24.68

30-24.68

请按任意键继续. . .

   #方法三:使用stringstream

Input/output string stream class: stringstream provides an interface to manipulate strings as if they were input/output streams.

 



Demo:

[cpp] view
plain copy

#include <iostream>   

#include <string>  

#include <sstream>    //引入stringstream头文件  

using namespace std;   

int main()   

{       

    stringstream strStream;    

    int a = 100;    

    float f = 23.5566;   

    //int、float类型都可以塞到stringstream中  

    strStream << a << "----"<< f ;   

    string s = strStream.str();    

    cout << s << endl;    

  

    system("pause");      

    return 0;    

}  

输出结果:

100----23.5566

请按任意键继续. . .

  #四、其他

1.sprintf可能引起缓冲区溢出,可以考虑使用 snprintf 或者非标准的 asprintf

2.如果是mfc程序,可以使用 CString::Format

3.如果使用boost,则可以直接使用: string s = boost::lexical_cast <string>(a);

4.atoi 也是不可移植的。

一、C++的int转string
 #方法一: 使用itoa函数:    char *  itoa ( int value, char * str, int base );                            
说明:Convert integer to string (non-standard function)
参数:
·value : Value to be converted to a string.·str : Array in memory where to store the resulting null-terminated string.·base : Numerical base used to represent the value as a string, between2 and36, where10 means decimal base, 16 hexadecimal,8 octal, and2 binary.
Demo:

[cpp] view
plain copy

#include <iostream>   

using namespace std;   

int main(int argc, char* argv[])    

{      

    int n = 30;   

    char c[10];     

    //二进制转换  

    itoa(n, c, 2);    

    cout << "2-> " << c << endl;   

    //十进制转换  

    itoa(n, c, 10);      

    cout << "10-> " <<  c << endl;  

    //十六进制转换  

    itoa(n, c, 16);       

    cout << "16-> " <<  c << endl;   

  

    system("pause");       

    return 0;    

}  

输出结果:

2-> 11110

10-> 30

16-> 1e

请按任意键继续. . .

  #方法二: 使用sprintf:   int sprintf ( char * str, const char * format, ... );

参数说明:

% 印出百分比符号,不转换。

b 整数转成二进位。

c 整数转成对应的 ASCII 字元。

d 整数转成十进位。

f 倍精确度数字转成浮点数。

o 整数转成八进位。

s 整数转成字串。

x 整数转成小写十六进位。

X 整数转成大写十六进位。

Demo:

[cpp] view
plain copy

#include <iostream>  

#include <string>  

using namespace std;    

int main()   

{       

    int n = 30;   

    char c[20];     //char *c;  

    //%d十进制  

    sprintf(c, "%d", n);     

    cout << c << endl;      

    //%o八进制  

    sprintf(c, "%o", n);     

    cout << c << endl;     

    //%X大写十六进制  

    sprintf(c, "%X", n);     

    cout << c << endl;    

    //%cACSII字元  

    sprintf(c, "%c", n);     

    cout << c << endl;    

      

    //%f浮点数转换  

    float f = 24.678;      

    sprintf(c, "%f", f);   

    cout << c << endl;     

    //%.2f"保留小数点后面两位  

    sprintf(c, "%.2f", f);      

    cout << c << endl;    

    //转换两个数  

    sprintf(c, "%d-%.2f", n, f);    

    cout << c << endl;    

  

    system("pause");      

    return 0;    

}    

输出结果:


30

36

1E

//注:这里是个特殊符号

24.677999

24.68

30-24.68

请按任意键继续. . .

   #方法三:使用stringstream

Input/output string stream class: stringstream provides an interface to manipulate strings as if they were input/output streams.

 



Demo:

[cpp] view
plain copy

#include <iostream>   

#include <string>  

#include <sstream>    //引入stringstream头文件  

using namespace std;   

int main()   

{       

    stringstream strStream;    

    int a = 100;    

    float f = 23.5566;   

    //int、float类型都可以塞到stringstream中  

    strStream << a << "----"<< f ;   

    string s = strStream.str();    

    cout << s << endl;    

  

    system("pause");      

    return 0;    

}  

输出结果:

100----23.5566

请按任意键继续. . .

  #四、其他

1.sprintf可能引起缓冲区溢出,可以考虑使用 snprintf 或者非标准的 asprintf

2.如果是mfc程序,可以使用 CString::Format

3.如果使用boost,则可以直接使用: string s = boost::lexical_cast <string>(a);

4.atoi 也是不可移植的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++