char* to int
const char* a = "123"; int b = atoi(a);- 手写atoi:
int atoi(const char* str) {
const char* p = str;
bool neg = false;
int res = 0;
if (*str == '-' || *str == '+') {
str++;
}
while (*str != 0) {
//可以在此处加判断是否当前char为数字
res = res * 10 + *str - 48;
str++;
}
if (*p == '-') {
res = -res;
}
return res;
}
int to char*
int a = 123;
// b此处不能使用char*,因为那是const的
char b[4];
_itoa_s(a, b, 10);
float to int
float f = 9.87;
// 默认向下取整
int a = (int)f;
// round(), floor(), ceil()
// round(f) = 10; 四舍五入
// floor(f) = 9; 向下取“地板”
// ceil(f) = 10; 向上取“天花板“
// 三个函数结果可以为int,也可以为float,double,long
int to float
int a = 10;
float f = (float)a;
255

被折叠的 条评论
为什么被折叠?



