数据类型转换
- int与char[]
cout << "int与char *" << endl;
int a2 = 3; //65 = 0x00000041
char b2[4];
b2[3] = '/n';
sprintf_s(b2, "%d ", a2);
cout << b2 << endl;
int idscds = atoi(b2); // i = 15
cout << idscds << endl;
- int与const char *
const char* pzstr_zj = "199asdas80527.shudhsaidh";
int i_zi = std::stoi(pzstr_zj);
std::cout << pzstr_zj << ": " << i_zi << '\n';
- int与string
string str_dec = "2001, A Space Odyssey";
string str_hex = "40c3";
string str_bin = "-10010110001";
string str_auto = "0x7f";
string str_zj = "199asdas80527.shudhsaidh";
string::size_type sz; // alias of size_t
int i_dec = stoi(str_dec, &sz);//头文件include <string>
int i_hex = stoi(str_hex, nullptr, 16);//stoi(字符串,起始位置,n进制),将 n 进制的字符串转化为十进制
int i_bin = stoi(str_bin, nullptr, 2);
int i_auto = stoi(str_auto, nullptr, 0);
int i_zistr = stoi(str_zj);
cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]\n";//substr主要功能是复制子字符串,要求从指定位置开始,默认到结尾
cout << str_hex << ": " << i_hex << '\n';
cout << str_bin << ": " << i_bin << '\n';
cout << str_auto << ": " << i_auto << '\n';
cout << str_zj << ": " << i_zistr << '\n';
int awwdew = 12394;
string sstring;
for (int k = awwdew % 10; awwdew > 0; awwdew /= 10, k = awwdew % 10)
sstring.push_back(k + '0');
reverse(sstring.begin(), sstring.end());
cout << sstring << endl;
string sto_string = to_string(awwdew);//将整数a转换为字符型
cout << sto_string << endl;
- double与char *
cout << "double与char *" << endl;
double d = 123456.1234567899;
char s[50];
sprintf_s(s, "%.10f", d);// 10f小数位数
printf("%s\n", s);
char dsas[] = "15.455";
double db;
db = atof(dsas); // db = 15.455
cout << db << endl;
- double与const char *
const char* dsasdssdd = "15.455";
double sdcfsdcf;
sdcfsdcf = atof(dsasdssdd); // db = 15.455
cout << sdcfsdcf << endl;
- double与string
string dsasstring = "15.455";
double sdcfsdcfstring;
sdcfsdcfstring = atof(dsasstring.c_str()); // db = 15.455
cout << sdcfsdcfstring << endl;
- double与int
double dfdouble = 8978.77878;
int jhddcjhsj = dfdouble;
double ndshchdsc = jhddcjhsj;
cout << "double与int" << endl;
cout << jhddcjhsj << endl;
cout << floor(dfdouble) << endl;//向下取整
cout << round(dfdouble) << endl; //四舍五入
cout << ceil(dfdouble) << endl;//向上取整
cout << ndshchdsc << endl;
- string与const char *
string str = "world";
//使用char * p=(char*)str.c_str()效果相同
const char* p = str.c_str();
cout << "string与const char*" << endl;
cout << p << endl;
string st1 = p;
cout << st1 << endl;
- char与string
string strstr = "d";
const char* p1 = str.c_str();
cout << "string与const char*" << endl;
cout << p1 << endl;
string st12 = p1;
cout << st12 << endl;
- char与转char*
char pp[20] = "helloworld";
const char* p12 = pp;
cout << "char与转char*" << endl;
cout << p12 << endl;
char arr[20];
strcpy_s(arr, p12);
cout << arr << endl;
- char[]转string
string strString;
char p2[20] = "helloworld";
strString = p2;
cout << "char[]转string" << endl;
cout << strString << endl;
char p3[20];
int i;
for (i = 0; i < strString.length(); i++)
p3[i] = strString[i];
p3[i] = '\0'; //添加结束符
cout << p3 << endl;