模仿/模仿C语言中的大端行为?

模仿/模仿C语言中的大端行为?,c,gcc,mingw,C,Gcc,Mingw,我想知道是否有可能为了测试目的而模仿big endian行为 通过windows或linux、mingw或gcc。下面是我希望仿真返回big-endian的代码示例: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #if CHAR_BIT != 8 #error "Unsupported char size for detecting

我想知道是否有可能为了测试目的而模仿big endian行为

通过windows或linux、mingw或gcc。下面是我希望仿真返回big-endian的代码示例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <limits.h>
#if CHAR_BIT != 8
#error "Unsupported char size for detecting endianness"
#endif

int main (void)
{
  short int word = 0x0001;
  char *byte = (char *) &word;
  if (byte[0]) printf("little endian");
  else printf("big endian");
  return 0;
}
#包括
#包括
#包括
#包括
#如果字符位!=8.
#错误“不支持用于检测endianness的字符大小”
#恩迪夫
内部主(空)
{
短整数字=0x0001;
字符*字节=(字符*)&字;
if(字节[0])printf(“小端点”);
else printf(“big endian”);
返回0;
}

为什么要在运行时检测平台的endianness?在进行编译时,endian模式是已知的。您的代码将执行您期望的操作。。。假设目标平台上的短int为2字节。为了避免这种挂起,最好查看最后一个字节。(byte[sizeof(short int)-1])

如果您真的想这样做,那么您可以使用Intel Mac并为x86和ppc构建。ppc可执行文件将通过Rosetta仿真运行,并且将是big-endian,而本机x86版本当然将是little-endian。

您可以在所有不同的使用之间抛出一系列hton*(主机到网络)和ntoh*(网络到主机)调用。网络端是大端

16位: htons=主机到网络短路 ntohs=网络对主机短路

32位: htonl=主机到网络的长度 ntohl=网络到主机长

实际上,主机到网络和网络到主机都是相同的,因为无论哪种方式都会发生相同的交换

它们通常以宏的形式实现,在使用big-endian的平台上不会执行操作

他们住在:

#include <arpa/inet.h> 
#包括
这是普遍可用的

如果您的库中有
,您也可以使用它。对于gcc,这需要
-D\u BSD\u源代码

在Unix、BSD和Linux上,请尝试: 曼顿 曼恩迪安
man字节顺序

您不能为了测试或类似的目的而切换Endian。您可以做的是,为big-endian体系结构安装一个仿真器,并为仿真器编译程序。下面是一种方式:

是各种QEMU支持架构的Debian磁盘映像。mips、sparc和arm都是big-endian(不要下载任何以-el结尾的内容)。我正在使用Debian Lenny进行MIPS()。为您的平台安装QEMU,然后按照MIPS页面上的说明下载映像和内核文件

现在,您可以从控制台直接启动Debian5 for MIPS。登录到您的虚拟机,成为超级用户(密码为“root”)并安装C编译器:

debian-mips:~# su -
debian-mips:~# apt-get update
debian-mips:~# apt-get install gcc
启动编辑器并输入您的程序:

debian-mips:~# pico foo.c
debian-mips:~# gcc foo.c
debian-mips:~# ./a.out
big endian

我想在我的小endian Intel机器上进行big-endian仿真,以测试程序的字节顺序相关问题。QEMU PowerPC模拟器似乎是一个很好的解决方案。我已经在下面记录了设置它的步骤

1) 安装QEMU

nifty:~# aptitude update && aptitude install qemu
2) 从Linux上下载Mac,并将下载中的“video.x”文件复制到“/usr/share/qemu”。这对于防止qemu系统ppc抱怨是必要的

nifty:~# tar -xjf mol-0.9.72.1.tar.bz2
nifty:~# cp mol-0.9.72.1/mollib/drivers/video.x /usr/share/qemu
3) 下载Debian for PowerPC并将其安装在QEMU硬盘映像上

susam@nifty:~/qemu$ wget --no-verbose http://cdimage.debian.org/debian-cd/5.0.4/powerpc/iso-cd/debian-504-powerpc-CD-1.iso
2010-06-19 02:55:06 URL:http://caesar.acc.umu.se/debian-cd/5.0.4/powerpc/iso-cd/debian-504-powerpc-CD-1.iso[675569664/675569664] -> "debian-504-powerpc-CD-1.iso" [1]
susam@nifty:~/qemu$ qemu-img create powerpc.img 2G
Formatting 'powerpc.img', fmt=raw size=2147483648
susam@nifty:~/qemu$ qemu-system-ppc -hda powerpc.img -cdrom debian-504-powerpc-CD-1.iso -boot d -m 512
susam@nifty:~/qemu$ qemu-system-ppc -hda powerpc.img -m 512
4) 使用硬盘映像启动QEMU PowerPC模拟器

susam@nifty:~/qemu$ wget --no-verbose http://cdimage.debian.org/debian-cd/5.0.4/powerpc/iso-cd/debian-504-powerpc-CD-1.iso
2010-06-19 02:55:06 URL:http://caesar.acc.umu.se/debian-cd/5.0.4/powerpc/iso-cd/debian-504-powerpc-CD-1.iso[675569664/675569664] -> "debian-504-powerpc-CD-1.iso" [1]
susam@nifty:~/qemu$ qemu-img create powerpc.img 2G
Formatting 'powerpc.img', fmt=raw size=2147483648
susam@nifty:~/qemu$ qemu-system-ppc -hda powerpc.img -cdrom debian-504-powerpc-CD-1.iso -boot d -m 512
susam@nifty:~/qemu$ qemu-system-ppc -hda powerpc.img -m 512
5) 通过编写一个简单的C程序来验证我是否真的使用了big-endian系统

susam@lilliput:~$ cat endian.c
#include <stdio.h>

int main()
{
    int n = 0x1;
    printf(*((char *) &n) ? "little-endian\n" : "big-endian\n");
    return 0;
}
susam@lilliput:~$ gcc endian.c && ./a.out
big-endian
susam@lilliput:~$ 
susam@lilliput:~$cat endian.c
#包括
int main()
{
int n=0x1;
printf(*(char*)&n)-“小端\n”:“大端\n”);
返回0;
}
susam@lilliput:~$gcc endian.c&&./a.out
大端
susam@lilliput:~$ 

如果你错过了双关语,小人国人本来就是大端人

这里有一个更详细的要求:我有一个将数据存储到文件的util。数据尊重本地环境的本地端性。现在,我希望这些数据在不同的环境下有效,这意味着它需要通过原始环境的指标来“调整”自身-我希望这是有意义的-最佳实践,在这种情况下,无论编写器的体系结构如何,文件格式都使用单端。您会发现有一些方法对此很有用:htons、htonl、ntohl、ntohs。([host | network]到[host | network][short | long])htons将进行一个short并将其转换为big-endian,而不考虑本地endianness。ntohl将使用一个big-endian-long并将其转换为本地字节顺序。我喜欢这些方法。他们是你的朋友。有几种文件格式的头文件中包含endianness,并且有库可以转换为endianness或至少从endianness转换。不过,这是一项工作。你有点破坏了我的目标!:)。。。我想学习这种方法,即使这样,将所有内容调整到固定端会更容易。Intel Mac是一种硬件?@Doori:是的,它是一种带有Intel CPU的Mac(与旧款Mac不同,后者有IBM/Motorola PowerPC CPU)。你知道基于软件的解决方案吗?是的,比如QEMU。请看我的答案。@DooriBar Rosetta也是一个基于软件的解决方案,因为x86 CPU无法运行ppc指令。我希望有一个“更干净”的解决方案。。。我不主动扮演创造角色的模拟。(为了更严格的验证目的)
int-output_32(FILE*f,uint32_-t x){if(io_-endian!=local_-endian){x=endian_-swap32(x);}return fwrite(&x,sizeof(uint32_-t),1,f)}
感谢您的回答,我还没有实际设置它,但我想它应该能满足我的需要。一些架构支持切换。ppc可以使用
-mlittle-endian
进行编译,但它同样无用,因为程序无法独立运行。