std::string转换为uint32_t or int string不能直接转int需要先转为char*
std::string str = "12";
uint32_t val = std::atoi(str.c_str());
int val = std::atoi(str.c_str());
char 转为int
char str[10]="121";
int num=atoi(str);
int 转为char 需要头文件#include<stdlib.h> 如果itoa报错则需要在项目预处理器中加入_CRT_NONSTDC_NO_DEPRECATE和_CRT_SECURE_NO_WARNINGS。 详见: itoa报错解决方法 https://blog.csdn.net/hou09tian/article/details/80616155
int num = 10;
char a[3];
itoa(num, a, 10);
std::string or char转double or float
std::string str = "12.32";
double val = std::atof(str.c_str());
float val = std::atof(str.c_str());
int or double转为 std::string
int a = 10;
std::string b = std::to_string(a);
double a = 12.22;
std::string b = std::to_string(a);
double转为char 需要头文件#include < sstream >
double d = 12.4500;
std::ostringstream oss1;
oss1 << d;
char fileName[50];
memset(fileName, 0, 50);
strcat(fileName, (oss1.str()).c_str());
for (int i = 0; i < strlen(fileName); ++i){
std::cout << fileName[i];
}
|