https://blog.csdn.net/vast_sea/article/details/8122213
https://www.linuxidc.com/Linux/2015-11/124918.htm
Linux内核中只有atoi()函数,被包含在stdlib.h头文件中,而没有itoa()函数,不过,itoa()函数的功能可以用sprintf()函数代替。如
int nValue = 80;
char* szBuffer = (char *)malloc(sizeof(int) + 1); //分配动态内存
memset(szBuffer, 0, sizeof(int) + 1); //内存块初始化
sprintf(szBuffer, "%d", nValue); //整数转化为字符串
free(szBuffer); //释放动态分配的内存
sprintf 跟printf 在用法上几乎一样,只是打印的目的地不同而已,前者打印到字符串中,后者则直接在命令行上输出。这也导致sprintf 比printf 有用得多。
sprintf 是个变参函数,定义如下:
int sprintf( char *buffer, const char *format [, argument] ... );
除了前两个参数类型固定外,后面可以接任意多个参数。而它的精华显然就在第二个参数:
格式化字符串上。
sprintf 最常见的应用之一莫过于把整数打印到字符串中,所以 spritnf 在大多数场合可以替代 itoa
以下是Windows/Linux系统中常用的C/C++各种数据类型转换汇总:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <string.h>
int main()
{
// 1--> int to char[]
int tmp1 = 100;
char ch1[15];
sprintf(ch1, "%d", tmp1);
std::cout<<ch1<<std::endl;
// 2--> int to string
int tmp2 = 111;
char ch2[15];
sprintf(ch2, "%d", tmp2);
std::string str2;
str2 = std::string(ch2);
std::cout<<str2<<std::endl;
// 3--> int to enum
enum enum3 {
A,
B
};
int tmp3 = 222;
enum3 val3 = static_cast<enum3>(tmp3);
std::cout<<val3<<std::endl;
// 4--> char[] to string
char arr4[] = "this is a sample";
std::string str4;
str4 = std::string(arr4);
std::cout<<str4<<std::endl;
// 5--> char to int
char ch5 = '8';
int val5 = ch5 - '0';// val5 is bounded by 0 to 9
std::cout<<val5<<std::endl;
// 6--> char[] to int
char arr6[] = "12345";
int tmp6;
sscanf(arr6, "%d", &tmp6);
std::cout<<tmp6<<std::endl;
// 7--> char* to int
char* pch7 = "444";
int tmp7;
tmp7 = atoi(pch7);
std::cout<<tmp7<<std::endl;
// 8--> char* to float
char* pch8 = "55.5";
float tmp8;
tmp8 = (float)atof(pch8);
std::cout<<tmp8<<std::endl;
// 9--> char* to double
char* pch9 = "66.666";
double tmp9;
tmp9 = atof(pch9);
std::cout<<tmp9<<std::endl;
// 10--> float to char[]
float tmp10 = 11.11;
char ch10[20];
sprintf(ch10, "%f", tmp10);
std::cout<<ch10<<std::endl;
// 11-> int to char*
int tmp11 = 777;
char* pch11;
char ch11[20];
sprintf(ch11, "%d", tmp11);
pch11 = ch11;
std::cout<<pch11<<std::endl;
// 12--> double to char[]
double tmp12 = 8.888;
char arr12[20];
sprintf(arr12, "%f", tmp12);
std::cout<<arr12<<std::endl;
// 13--> char* to string
char* pch13 = "hello, world";
std::string str13;
str13 = std::string(pch13);
std::cout<<str13<<std::endl;
// 14--> string to char[]
std::string str14 = "dog, cat";
char arr14[255];
strncpy(arr14, str14.c_str(), sizeof(arr14));
arr14[sizeof(arr14) - 1] = 0;
std::cout<<arr14<<std::endl;
// 15--> string to const char*
std::string str15 = "ha ha";
const char* pch15;
pch15 = str15.c_str();
std::cout<<pch15<<std::endl;
// 16--> float to int
float ftmp16 = 99.99;
int tmp16;
tmp16 = static_cast<int>(ftmp16);// static_cast<int>(ftmp16 + 0.5)
std::cout<<tmp16<<std::endl;
return 0;
}
int atoi ( const char * str );
Convert string to integer
Parses the C string str interpreting its content as an integral number, which is returned as an int value.
/* atoi example */#include <stdio.h>#include <stdlib.h>
int main ()
{
int
i;
char
szInput [256];
printf (
"Enter a number: "); fgets ( szInput, 256, stdin ); i = atoi (szInput); printf ("The value entered is %d. The double is %d.\n",i,i*2); return 0; }
output
Enter a number: 73
The value entered is 73. The double is 146.
long int atol ( const char * str );
Convert string to long integer
Parses the C string str interpreting its content as an integral number, which is returned as a long int value.
/* atol example */#include <stdio.h>#include <stdlib.h>
int main ()
{
long
int
li;
char
szInput [256];
printf (
"Enter a long number: "); gets ( szInput ); li = atol (szInput); printf ("The value entered is %d. The double is %d.\n",li,li*2); return 0; }
output
Enter a number: 567283
The value entered is 567283. The double is 1134566.
double atof ( const char * str );
Convert string to double
Parses the C string str interpreting its content as a floating point number and returns its value as a double.
/* atof example: sine calculator */#include <stdio.h>#include <stdlib.h>#include <math.h>
int main ()
{
double
n,m;
double
pi=3.1415926535;
char
szInput [256];
printf (
"Enter degrees: " ); gets ( szInput ); n = atof ( szInput ); m = sin (n*pi/180); printf ( "The sine of %f degrees is %f\n" , n, m ); return 0; } OutputEnter degrees: 45
The sine of 45.000000 degrees is 0.707101
char * itoa ( int value, char * str, int base );
Convert integer to string (non-standard function)
Converts an integer value to a null-terminated string using the specified base and stores the result in the array given by str parameter.
This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.
A standard-compliant alternative for some cases may be sprintf:
- sprintf(str,"%d",value) converts to decimal base.
- sprintf(str,"%x",value) converts to hexadecimal base.
- sprintf(str,"%o",value) converts to octal base
/* itoa example */#include <stdio.h>#include <stdlib.h>
int main ()
{
int
i;
char
buffer [33];
printf (
"Enter a number: "); scanf ("%d",&i); itoa (i,buffer,10); printf ("decimal: %s\n",buffer); itoa (i,buffer,16); printf ("hexadecimal: %s\n",buffer); itoa (i,buffer,2); printf ("binary: %s\n",buffer); return 0; }
Output
Enter a number: 1750
decimal: 1750
hexadecimal: 6d6
binary: 11011010110
#include <sstream>
string convertDouble(
doublevalue) {
std::ostringstream o;
if
(!(o << value))
return
""; return o.str(); }