标签:blog http io ar os sp on 2014 log
十六进制数相等的判断
请问如下程序的输出是神马?
#include <iostream> #include <string> using namespace std; int main (int, char *[]) { char s[1]={0}; s[0] = 0xfe; if (s[0] == 0xfe) { cout<<"=="<<endl; } else { cout<<"!="<<endl; } return 0; }
为何不相等呢?
看截图:
正确的做法:
#include <iostream> #include <string> using namespace std; int main (int, char *[]) { char s[1]={0}; s[0] = (char)0xfe;// s[0] = fe , s[0] < 0 if (s[0] == (char)0xfe) //禁止类型转换到int { cout<<"=="<<endl; } else { cout<<"!="<<endl; } return 0; }
标签:blog http io ar os sp on 2014 log
原文地址:http://blog.csdn.net/calmreason/article/details/41085115