返回信息流我试过了,atoi 还有int强制转化都不行。还有什么方法?
这是一条镜像帖。来源:北邮人论坛 / cpp / #42707同步于 2010/8/25
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
VC++ 中怎样将char 变成 int ?
zyy08
2010/8/25镜像同步19 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
char a;
int b = (int)a_char_variable;
int c = int(a_char_variable);
char *d = "12345";
int e = atoi(d);
char *f = "123456andsomegarbage";
char *f_end;
int g;
g = strtol(f, &f_end, 10);
CString h="12345";
int i = atoi(h); // Implicit conversion from CString to char*
没看懂是什么意思?
【 在 wks 的大作中提到: 】
: char a;
: int b = (int)a_char_variable;
: int c = int(a_char_variable);
: ...................
char a='a'; // 这是一个char型变量
int b = (int)a_char_variable; // 强制转换,b是a的ASCII码
int c = int(a_char_variable); // 和上一行一样。C++的语法
char *d = "12345"; // d是一个字符串(char指针,指向一个字符数组,以'\0'终止)
int e = atoi(d); // C语言stdlib.h里的字符串到整数的转换函数
char *f = "123456andsomegarbage"; // f也是一个字符串,只不过结尾有一些别的字符
char *f_end; // 用于存储数字部分的末尾
int g = strtol(f, &f_end, 10); // strtol是另一个转换函数,适用这种有别的字符的情况,还可以指定是10进制。
CString h="12345"; // h是MFC里面的CString类型
int i = atoi(h); // CString支持从CString到char*的隐式转换。当char*用就可以了。
string j="12345"; // C++标准库的string类型
int k = atoi(j.c_str()); // 这个要用string::c_str()函数得到适用C的函数
istringstream l(j); // C++的字符串输入流,把字符串像cin一样处理。l是一个流,内容初始化为j的内容
int m;
l>>m; // 从l读入一个整数m
现在是这样的:
有一个字符数组 char temp[10];现在想将其中的第5位赋给一个整形数 int a;这该怎么弄?前面用过强制转化和atoi都不行。
【 在 wks 的大作中提到: 】
: char a;
: int b = (int)a_char_variable;
: int c = int(a_char_variable);
: ...................
a = temp[4]; // 这样,a的值是temp[4]的ASCII码。
a = (int)temp[4]; // 也行,显式转换一下。
【 在 zyy08 的大作中提到: 】
: 现在是这样的:
: 有一个字符数组 char temp[10];现在想将其中的第5位赋给一个整形数 int a;这该怎么弄?前面用过强制转化和atoi都不行。
: 【 在 wks 的大作中提到: 】
: ...................
a里面是十六进制数,直接强制转化结果是负数。
【 在 wks 的大作中提到: 】
: a = temp[4]; // 这样,a的值是temp[4]的ASCII码。
: a = (int)temp[4]; // 也行,显式转换一下。
char *temp="deadbeef123456789abcdef";
int a = temp[4];
if(a>'9') {
a = a - 'a' + 10;
} else {
// a = a - '0' + 0;
a = a - '0';
}
【 在 zyy08 的大作中提到: 】
: a里面是十六进制数,直接强制转化结果是负数。
: 【 在 wks 的大作中提到: 】
: : a = temp[4]; // 这样,a的值是temp[4]的ASCII码。
: ...................