版本:vc6 系统:win7#include <iostream>
//#include <cmath>using namespace std;int main()//小型测试专用工程
{
char buff[4];
int d = 0x0002DA2C;
memcpy(buff, &d, 4); for(int i=0;i<=3;i++)
cout<<hex<<(int)buff[i]<<endl; system("pause");
return 0;
}
如图,buff[1]显示的是-38而不是218,即是0xFFFFFFDA而不是0x000000DA?求解...

解决方案 »

  1.   

    218已经大于你的char的取值范围了,
    unsigned char buff[4]; 应该不会出错。
      

  2.   

    char是带符号的,大于0的部分只到127,超过后就是负数了,所以-38很正常。想要0-255,就需要用BYTE
      

  3.   

    0xDA(16) = 11011010(2) = -(~(1011010(2))+1) = -(0100101(2)+1) = -(37+1) = -38
      

  4.   

    -38 看就什么类型而言了,如果是 4 字节,那么必然是 0xFFFFFFDA。