目录
double(float)与char*(char[])相互转换
前言
在编程过程中,经常会用到基本数据类型的转换,特此整理了int, double, char, char[], char*, string等C++基本数据类型之间的相互转换方式,以及一些解释说明,如果有不全之处或任何建议,欢迎各位朋友们在评论中指出,谢谢大家。
int与char相互转换
目标:将int型数字0到9中的某个数字转换成char字符'0'到'9',或者将char字符'0'到'9'转换成数字0到9。
int转char
int i = 6;
char c = i + '0'; // '6'
char转int
char c = '6';
int i = c - '0'; // 6
这里简单解释一下,这里涉及到的运算是ASCII码值之间的运算,c - '0'也就是'6' - '0','0'和'6'对应的ASCII码值分别为48和54(字符'0'到'9'的ASCII码值是连续增加的),所以这里 i = 54 - 48,但我们并不关心'0'和'6'具体的ASCII码值是多少,我们只需要知道'6'和'0'之间的差值是多少,所以在将int转成char时,我们只需要加'0',同理char转成int时,减'0'就可以。
char*与char[]相互转换
char*转char[]
char arr[3];
char *star = "Hi";
strcpy(arr, star);
strcpy函数的头文件需要 #include <string.h>或者 #include <cstring>,需要注意的是,在这个例子中char arr[3]这个数组大小,至少要为"Hi"的长度+1(需要多留一个'\0'的位置)= 3,若char arr[2]则会出错。
char[]转char*
char arr[] = "Hi";
char *star = arr;
int与char*(char[])相互转换
char*(char[])转int
方法1:sscanf函数,头文件 #include <stdio.h>或 #include <cstdio>
char *s = "-100"; // 或 char s[] = "-100"
int n;
sscanf(s, "%d", &n); // n = -100
方法2:atoi函数,头文件 #include <stdlib.h>或 #include <cstdlib>
char *s = "-100"; // 或 char s[] = "-100";
int n = atoi(s); // n = -100
方法3:strtod函数,该函数返回值为double型,头文件 #include <stdlib.h>或 #include <cstdlib>
char *s = "-100"; // 或 char s[] = "-100";
int n = strtod(s, NULL); // n = -100,这里有一个double到int的隐式转换
int转char*(char[])
方法1:sprintf函数,头文件 #include <stdio.h>或 #include <cstdio>
char *s = new char[5]; // 或 char s[5];
int n = -100;
sprintf(s, "%d", n);
char *申请的空间大小至少为要转换的数字的位数加1,比如转换数字-100,char *大小至少为4+1。否则会有警告:“'sprintf' writing a terminating nul past the end of the destination”。
方法2:Linux不支持,仅限Windows,itoa函数(或_itoa),头文件 #include <stdlib.h>或 #include <cstdlib>
char *s = new char[4]; // 或 char s[4];
int n = 100;
itoa(n, s, 10); // 第三个参数为多少进制,这里需要十进制
string与char*(char[])相互转换
string转char*(char[])
方法1:std::string的内联函数data(),返回值为const char *,从C++17开始可以返回非const指针,那么就可以通过该指针来修改string的内容。
std::string s = "Hi";
const char *cp = s.data(); // 可以用作const指针
char *p = s.data(); // 也可以用作非const指针
p[0] = 'A'; // 修改第一个字符
std::cout << s << std::endl; // Ai
方法2:std::string的内联函数c_str(),返回值为const char *,不可用作非const指针。
std::string s = "Hi";
char *p = s.c_str(); // 报错
const char *cp = s.c_str(); // 正确
方法3:std::string的copy函数
std::string str = "Hello";
char *s = new char[str.size() + 1]; // 或 char pp[str.size() + 1]
str.copy(s, str.size(), 0);
*(s + str.size()) = '\0'; // 要手动赋值最后一个字符为'\0',否则输出时可能为乱码
std::cout << s << std::endl; // Hello
copy函数声明为:std::size_t std::string::copy(char *__s, std::size_t __n, std::size_t __pos) const第一个参数是输出的char*,第二个参数是要复制的字符个数,第三个参数是从string的哪个index开始复制,这里要从string的index = 0开始,复制全部string,所以第二个参数是str.size(),第三个参数是从index = 0开始复制。
方法4:strcpy函数,详见 char*转char[]
char s[3];
std::string str = "Hi";
strcpy(s, str.c_str());
char*(char[])转string
直接赋值
char *s1 = "Hi";
std::string str1 = s1;
char s2[] = "Hi";
std::string str2 = s2;
int与string相互转换
int转string
方法1:std::to_string函数,头文件 #include <string>
int n = -100;
std::string s = std::to_string(n);
方法2:std::stringstream,头文件 #include <sstream>
int n = -100;
std::stringstream ss;
std::string s;
ss << n;
ss >> s;
string转int
方法1:std::stringstream,
std::string s = "-100";
std::stringstream ss;
int n;
ss << s;
ss >> n;
方法2:std::stoi函数,头文件 #include <string>
std::string s = "-100";
int i = std::stoi(s);
方法3,4:sscanf函数,atoi函数 详见 char*(char[])转int
std::string s = "-100";
// sscanf
int n1;
sscanf(s.c_str(), "%d", &n1);
// atoi
int n2 = atoi(str.c_str());
int与double(float)相互转换
int转double(float)
int i = -100;
float f1 = i; // -100
float f1 = (float)i; // -100
double d1 = i; // -100
double d2 = (double)i; // -100
double(float)转int
1. 丢失精度(丢失小数位)
double d = -2.6;
int i1 = d; // -2
int i2 = (int)d; // -2
2. 四舍五入
double d1 = 2.9;
double d2 = 2.1;
char s[10];
sprintf(s, "%.0f", d1); // 通过%m.nf这种方式可以做到四舍五入
int i1 = atoi(s); // 3
sprintf(s, "%.0f", d2);
int i2 = atoi(s); // 2
3. 上下取整
ceil()函数向上取整(ceill译为天花板),floor()函数向下取整(floor译为地板)。头文件 #include <math.h> 或者 #include <cmath>
double d = 2.5;
int i1 = std::ceil(d); // 3, 返回类型为double,隐式转成int
int i2 = std::floor(d); // 2, 返回类型为double,隐式转成int
若为负数,cell(-2.5) = -2,floor(-2.5) = -3
double(float)与char*(char[])相互转换
char*(char[])转double(float)
方法1:sscanf函数,头文件 #include <stdio.h>或 #include <cstdio>
char s[] = "2.5"
double d;
sscanf(s, "%lf", &d); // %lf表示输出double
float f;
sscanf(s, "%f", &f); // %f表示输出float
方法2:atof函数,返回类型为double,头文件 #include <stdlib.h>或 #include <cstdlib>
char s[] = "2.5";
double d = atof(s);
方法3:strtod函数,返回类型为double,头文件 #include <stdlib.h>或 #include <cstdlib>
char s[] = "2.5";
double d = strtod(s, NULL);
double(float)转char*(char[])
sprintf函数,头文件 #include <stdio.h>或 #include <cstdio>
char s[10];
double d = 2.5;
sprintf(s, "%lf", d);
float f = 2.5;
sprintf(s, "%f", f);
double(float)与string相互转换
double(float)转string
std::to_string函数,头文件 #include <string>
double d = 2.5;
std::string s1 = std::to_string(d);
float f = 2.5;
std::string s2 = std::to_string(f);
string转double(float)
方法1:std::stod,std::stof函数,头文件 #include <string>
std::string s = "2.5";
double d = std::stod(s);
float f = std::stof(s);
方法2,3,4:sscanf函数,atof函数,strtod函数,详见 char*(char[])转double(float)
std::string s = "2.5";
// sscanf
double d1;
sscanf(s.c_str(), "%lf", &d1);
// atof
double d2 = atof(s.c_str());
总结
如果本文对朋友们有帮助,还请点赞收藏评论,谢谢大家!!