【发布时间】:2019-11-05 02:44:18
【问题描述】:
int in=0xeef01f88;
int * ipoi=∈
char*cpoi=(char*)ipoi;
cout<<std::hex<<(int)*ipoi<<" "<<(int)*(cpoi)<<" "<<(int)*(cpoi+1)<<" "<<(int)*(cpoi+2)<<endl;
当我在 Cxxdroid(Android 编译器)上编译它时,它可以完美运行,因此 char 指针的 cout 给出了单独字节的值,例如:
eef01f88 88 1f f0.
当我使用 GNU GCC COMPILER 在 Windows 上编译相同的代码时,我得到了以下输出:
eef01f88 ffffff88 1f fffffff0.
这怎么可能?为什么第一个和第三个 char 指针值本身有 ffffff? *(cpoi+1) 周围甚至没有任何 ff 值。 (f0)
更改编译器会解决问题吗?
我正在使用代码块。它有很多编译选项,但我已经尝试了所有可用的标准(直到 2011 ISO C 语言标准)。我错过了什么?
【问题讨论】:
-
int * to char * 因为两个指针都可以指向内存中的同一个字节。正如您所看到的,两个系统(android windows)具有相同的位层次结构,因此代码应该工作相同。为了让你相信它是可能的,我什至根据 casts string int. 编写了 int 字符串
-
string potential_frames(char * source,long long length){ string str;长长我; ... char * i_c=(char*) &i; str=str+*(i_c+3); str=str+*(i_c+2); str=str+*(i_c+1); str=str+*(i_c);返回字符串; int in=((int)i_str[n*4])*256*256*256+((int)i_str[n*4+1])*256*256+((int)i_str[n*4+ 2])*256+((int)i_str[n*4+3]);它可以完美地在 android 上运行。
-
建议添加语言标签。您在问题中提到了 ISO C,但代码示例
cout << std::hex建议您尝试使用 C++(与 C 不同的语言) -
因为 (1) chars 是有符号的,(2) 0x88 是负数,(3) char 被提升为
int,以及 (4) 提升是符号扩展。
标签: c++ pointers casting char int