十六进制数相等的判断
请问如下程序的输出是神马?
#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;
}
原文:http://blog.csdn.net/calmreason/article/details/41085115