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

MSDN的例子
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*ITOA.C:Thisprogramconvertsintegersofvarious
*sizestostringsinvariousradixes.
*/
#include<stdlib.h>
#include<stdio.h>
voidmain( void )
{
charbuffer[20];
inti=3445;
longl=-344115L;
unsignedlongul=1234567890UL;
_itoa(i,buffer,10);
printf ( "Stringofinteger%d(radix10):%s\n" ,i,buffer);
_itoa(i,buffer,16);
printf ( "Stringofinteger%d(radix16):0x%s\n" ,i,buffer);
_itoa(i,buffer,2);
printf ( "Stringofinteger%d(radix2):%s\n" ,i,buffer);
_ltoa(l,buffer,16);
printf ( "Stringoflongint%ld(radix16):0x%s\n" ,l,buffer);
_ultoa(ul,buffer,16);
printf ( "Stringofunsignedlong%lu(radix16):0x%s\n" ,ul,buffer);
}
1
2
3
4
5
6
Output
Stringofinteger3445(radix10):3445
Stringofinteger3445(radix16):0xd75
Stringofinteger3445(radix2):110101110101
Stringoflongint-344115(radix16):0xfffabfcd
Stringofunsignedlong1234567890(radix16):0x499602d2
指定要转换的进制的基数,其值好象在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 转换成数字后相加。
    
    
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main ()
  4. {
  5.     int i;
  6.     char buffer[256];
  7.     printf ("Enter a number: ");
  8.     fgets (buffer, 256, stdin);
  9.     i = atoi (buffer);
  10.     printf ("The value entered is %d.", i);
  11.     system("pause");
  12.     return 0;
  13. }
执行结果:
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, between 2 and 36, where 10 means decimal base,  16 hexadecimal, 8 octal, and 2 binary.

Demo:

[cpp]  view plain  copy
  1. #include <iostream>   
  2. using namespace std;   
  3. int main(int argc, char* argv[])    
  4. {      
  5.     int n = 30;   
  6.     char c[10];     
  7.     //二进制转换  
  8.     itoa(n, c, 2);    
  9.     cout << "2-> " << c << endl;   
  10.     //十进制转换  
  11.     itoa(n, c, 10);      
  12.     cout << "10-> " <<  c << endl;  
  13.     //十六进制转换  
  14.     itoa(n, c, 16);       
  15.     cout << "16-> " <<  c << endl;   
  16.   
  17.     system("pause");       
  18.     return 0;    
  19. }  

输出结果:

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
  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std;    
  4. int main()   
  5. {       
  6.     int n = 30;   
  7.     char c[20];     //char *c;  
  8.     //%d十进制  
  9.     sprintf(c, "%d", n);     
  10.     cout << c << endl;      
  11.     //%o八进制  
  12.     sprintf(c, "%o", n);     
  13.     cout << c << endl;     
  14.     //%X大写十六进制  
  15.     sprintf(c, "%X", n);     
  16.     cout << c << endl;    
  17.     //%cACSII字元  
  18.     sprintf(c, "%c", n);     
  19.     cout << c << endl;    
  20.       
  21.     //%f浮点数转换  
  22.     float f = 24.678;      
  23.     sprintf(c, "%f", f);   
  24.     cout << c << endl;     
  25.     //%.2f"保留小数点后面两位  
  26.     sprintf(c, "%.2f", f);      
  27.     cout << c << endl;    
  28.     //转换两个数  
  29.     sprintf(c, "%d-%.2f", n, f);    
  30.     cout << c << endl;    
  31.   
  32.     system("pause");      
  33.     return 0;    
  34. }    

输出结果:

30  
36  
1E  
//注:这里是个特殊符号  
24.677999  
24.68  
30-24.68  
请按任意键继续. . .
   #方法三:使用stringstream

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

 

Demo:

[cpp]  view plain  copy
  1. #include <iostream>   
  2. #include <string>  
  3. #include <sstream>    //引入stringstream头文件  
  4. using namespace std;   
  5. int main()   
  6. {       
  7.     stringstream strStream;    
  8.     int a = 100;    
  9.     float f = 23.5566;   
  10.     //int、float类型都可以塞到stringstream中  
  11.     strStream << a << "----"<< f ;   
  12.     string s = strStream.str();    
  13.     cout << s << endl;    
  14.   
  15.     system("pause");      
  16.     return 0;    
  17. }  

输出结果:

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, between 2 and 36, where 10 means decimal base,  16 hexadecimal, 8 octal, and 2 binary.

Demo:

[cpp]  view plain  copy
  1. #include <iostream>   
  2. using namespace std;   
  3. int main(int argc, char* argv[])    
  4. {      
  5.     int n = 30;   
  6.     char c[10];     
  7.     //二进制转换  
  8.     itoa(n, c, 2);    
  9.     cout << "2-> " << c << endl;   
  10.     //十进制转换  
  11.     itoa(n, c, 10);      
  12.     cout << "10-> " <<  c << endl;  
  13.     //十六进制转换  
  14.     itoa(n, c, 16);       
  15.     cout << "16-> " <<  c << endl;   
  16.   
  17.     system("pause");       
  18.     return 0;    
  19. }  

输出结果:

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
  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std;    
  4. int main()   
  5. {       
  6.     int n = 30;   
  7.     char c[20];     //char *c;  
  8.     //%d十进制  
  9.     sprintf(c, "%d", n);     
  10.     cout << c << endl;      
  11.     //%o八进制  
  12.     sprintf(c, "%o", n);     
  13.     cout << c << endl;     
  14.     //%X大写十六进制  
  15.     sprintf(c, "%X", n);     
  16.     cout << c << endl;    
  17.     //%cACSII字元  
  18.     sprintf(c, "%c", n);     
  19.     cout << c << endl;    
  20.       
  21.     //%f浮点数转换  
  22.     float f = 24.678;      
  23.     sprintf(c, "%f", f);   
  24.     cout << c << endl;     
  25.     //%.2f"保留小数点后面两位  
  26.     sprintf(c, "%.2f", f);      
  27.     cout << c << endl;    
  28.     //转换两个数  
  29.     sprintf(c, "%d-%.2f", n, f);    
  30.     cout << c << endl;    
  31.   
  32.     system("pause");      
  33.     return 0;    
  34. }    

输出结果:

30  
36  
1E  
//注:这里是个特殊符号  
24.677999  
24.68  
30-24.68  
请按任意键继续. . .
   #方法三:使用stringstream

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

 

Demo:

[cpp]  view plain  copy
  1. #include <iostream>   
  2. #include <string>  
  3. #include <sstream>    //引入stringstream头文件  
  4. using namespace std;   
  5. int main()   
  6. {       
  7.     stringstream strStream;    
  8.     int a = 100;    
  9.     float f = 23.5566;   
  10.     //int、float类型都可以塞到stringstream中  
  11.     strStream << a << "----"<< f ;   
  12.     string s = strStream.str();    
  13.     cout << s << endl;    
  14.   
  15.     system("pause");      
  16.     return 0;    
  17. }  

输出结果:

100----23.5566  
请按任意键继续. . .

  #四、其他

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

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

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

4.atoi 也是不可移植的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值