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
|
sprintf(str, "%x", 100); //将100转为16进制表示的字符串。
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, between 2 and 36, where 10 means decimal base, 16 hexadecimal, 8 octal, and 2 binary.Demo:
输出结果:
2-> 11110 10-> 30 16-> 1e 请按任意键继续. . . |
#方法二: 使用sprintf: int sprintf ( char * str, const char * format, ... );
参数说明:
% 印出百分比符号,不转换。 b 整数转成二进位。 c 整数转成对应的 ASCII 字元。 d 整数转成十进位。 f 倍精确度数字转成浮点数。 o 整数转成八进位。 s 整数转成字串。 x 整数转成小写十六进位。 X 整数转成大写十六进位。 |
Demo:
输出结果:
30 36 1E //注:这里是个特殊符号 24.677999 24.68 30-24.68 请按任意键继续. . . |
Input/output string stream class: stringstream provides an interface to manipulate strings as if they were input/output streams.
Demo:
输出结果:
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:
输出结果:
2-> 11110 10-> 30 16-> 1e 请按任意键继续. . . |
#方法二: 使用sprintf: int sprintf ( char * str, const char * format, ... );
参数说明:
% 印出百分比符号,不转换。 b 整数转成二进位。 c 整数转成对应的 ASCII 字元。 d 整数转成十进位。 f 倍精确度数字转成浮点数。 o 整数转成八进位。 s 整数转成字串。 x 整数转成小写十六进位。 X 整数转成大写十六进位。 |
Demo:
输出结果:
30 36 1E //注:这里是个特殊符号 24.677999 24.68 30-24.68 请按任意键继续. . . |
Input/output string stream class: stringstream provides an interface to manipulate strings as if they were input/output streams.
Demo:
输出结果:
100----23.5566 请按任意键继续. . . |
#四、其他
1.sprintf可能引起缓冲区溢出,可以考虑使用 snprintf 或者非标准的 asprintf
2.如果是mfc程序,可以使用 CString::Format
3.如果使用boost,则可以直接使用: string s = boost::lexical_cast <string>(a);
4.atoi 也是不可移植的。