模仿/模拟在C大端行为? [英] Imitate/emulate a big-endian behavior in C?
问题描述
我想知道是否有可能仿效大端行为,测试目的是什么?
通过Windows或Linux,MinGW的GCC或。这里,我想仿真返回大端code的样本:
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;#包括LT&;&limits.h中GT;
#如果CHAR_BIT!= 8
#ERROR不支持的字符尺寸检测字节顺序
#万一INT主要(无效)
{
短整型字= 0×0001;
字符*字节=(字符*)及字;
如果(字节[0])的printf(小尾数);
其他的printf(大端);
返回0;
}
您不能切换用于测试目的或类似的东西endianes。你可以做的是,要安装模拟器大端架构和编译程序的仿真器。这里有一种方法,下:
http://people.debian.org/~aurel32/qemu/
对于各种QEMU的Debian的磁盘映像支持的体系。 MIPS,SPARC和手臂是大端(不下载任何与-el结尾)。我使用Debian Lenny的为MIPS( http://people.debian.org/~aurel32/qemu/mips/ )。 QEMU安装您的平台,然后按照MIPS-页面上的说明下载图像和内核文件。
现在,你可以启动到从您的控制台Debian的5 MIPS的权利。登录到你的虚拟机上,成为超级用户(密码为根),并安装C编译器:
的debian-MIPS:〜#苏 -
Debian的MIPS:〜#apt-get的更新
Debian的MIPS:〜#apt-get的安装gcc
打开编辑器,然后输入你的程序:
的debian-MIPS:〜#微微的foo.c
Debian的MIPS:〜#GCC的foo.c
Debian的MIPS:〜#./a.out
大端
I was wondering if it's possible to emulate a big-endian behavior, for testing purpose?
via either windows or linux , mingw or gcc. Here's a sample of code which I would like the emulation to return 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;
}
You can't switch endianes for testing purposes or anything like that. What you can do is, to install an emulator for a big-endian architecture and compile your program for the emulator. Here's one way, under:
http://people.debian.org/~aurel32/qemu/
are Debian disk images for all kinds of QEMU supported architectures. mips, sparc and arm are big-endian (do not download anything ending with -el). I'm using Debian Lenny for MIPS ( http://people.debian.org/~aurel32/qemu/mips/ ). Install QEMU for your platform, then follow the instructions on the MIPS-page to download a image and kernel file.
Now you can boot into a Debian 5 for MIPS right from your console. Login to you virtual machine, become super user (the password is "root") and install the C-compiler:
debian-mips:~# su -
debian-mips:~# apt-get update
debian-mips:~# apt-get install gcc
fire up an editor and enter your program:
debian-mips:~# pico foo.c
debian-mips:~# gcc foo.c
debian-mips:~# ./a.out
big endian
这篇关于模仿/模拟在C大端行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!