恩迪安独立 [英] Endian Independence

查看:50
本文介绍了恩迪安独立的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>


#define LITTLE_ENDIAN 0

#define BIG_ENDIAN 1


int endian(){

int i = 1;

char * p =(char *)& i;


if(p [0] == 1)

返回LITTLE_ENDIAN;

else

返回BIG_ENDIAN;

}

int reverseInt(int i){

unsigned char c1,c2,c3,c4;


if(endian() == BIG_ENDIAN){

返回i;

}否则{

c1 = i& 255;

c2 =(i> 8)& 255;

c3 =(i> 16)& 255;

c4 =(i> 24)& 255;


return((int)c1<< 24)+((int)c2<< 16)+((int)c3<< 8) + c4;

}

}


int main(无效)

{

if(endian())

puts(Big Endian Machine);

else

puts(" Small Endian Machine");

printf("%d",reverseInt(5));

返回0;


}


我在我的电脑上测试它(在奔腾4上),这是输出:


小端机器

83886080.


我感到困惑,因为我预计要打印5张或者是否我完全错过了什么?b / b $ b / b >
可能我完全误解了字节序的概念:(


感谢任何帮助。


谢谢

#include<stdio.h>

#define LITTLE_ENDIAN 0
#define BIG_ENDIAN 1

int endian() {
int i = 1;
char *p = (char *)&i;

if (p[0] == 1)
return LITTLE_ENDIAN;
else
return BIG_ENDIAN;
}
int reverseInt (int i) {
unsigned char c1, c2, c3, c4;

if ( endian() == BIG_ENDIAN ) {
return i;
} else {
c1 = i & 255;
c2 = (i >8) & 255;
c3 = (i >16) & 255;
c4 = (i >24) & 255;

return ((int)c1 << 24) + ((int)c2 << 16) + ((int)c3 << 8) + c4;
}
}

int main(void)
{
if(endian())
puts("Big Endian Machine");
else
puts("Small Endian Machine");
printf("%d",reverseInt(5));
return 0;

}

I tested it on my PC (On Pentium 4) and this is the output:

Small Endian Machine
83886080.

I am baffled as I was expecting 5 to be printed or is it that I am
missing something completely ?
Probably i have completely misunderstood the idea of endianness :(

Any help is appreciated.

Thank You

推荐答案

2008年7月27日15:41,Kelly B写道:
On 27 Jul 2008 at 15:41, Kelly B wrote:

int reverseInt(int i){
unsigned char c1,c2,c3,c4;


if(endian()== BIG_ENDIAN){

return i;

}否则{

c1 = i& 255;

c2 =(i> 8)& 255;

c3 =(i> 16)& 255;

c4 =(i> 24)& 255;


return((int)c1<< 24)+((int)c2<< 16)+((int)c3<< 8) + c4;

}

}


int main(无效)

{

if(endian())

puts(Big Endian Machine);

else

puts(" Small Endian Machine");

printf("%d",reverseInt(5));

返回0;

}


我在我的电脑上测试它(在奔腾4上)这是输出:


小端机器

83886080.


我感到困惑,因为我期待打印5或者是因为我完全错过了什么?b
$ b
int reverseInt (int i) {
unsigned char c1, c2, c3, c4;

if ( endian() == BIG_ENDIAN ) {
return i;
} else {
c1 = i & 255;
c2 = (i >8) & 255;
c3 = (i >16) & 255;
c4 = (i >24) & 255;

return ((int)c1 << 24) + ((int)c2 << 16) + ((int)c3 << 8) + c4;
}
}

int main(void)
{
if(endian())
puts("Big Endian Machine");
else
puts("Small Endian Machine");
printf("%d",reverseInt(5));
return 0;
}

I tested it on my PC (On Pentium 4) and this is the output:

Small Endian Machine
83886080.

I am baffled as I was expecting 5 to be printed or is it that I am
missing something completely ?



你的(命名稍差)reverseInt函数接受一个整数i,并且

返回32位,当解释为bigendian整数时给出i 。

由于你的机器是littleendian,printf()函数解释它的

参数就像它们是littleendian一样,所以当你传递printf

reversInt(5)作为参数,它将其解释为0x05000000。

Your (somewhat poorly named) reverseInt function takes an integer i, and
returns 32 bits that give i when interpreted as a bigendian integer.
Since your machine is littleendian, the printf() function interprets its
arguments as if they were littleendian, so when you pass printf
reversInt(5) as an argument, it interprets this as 0x05000000.


Kelly B< ke **** @ gmail.comwrites:


< snip>
Kelly B <ke****@gmail.comwrites:

<snip>

这就是我的错误:(

http://www.ibm.com/developerworks/ai...ry/au-endianc/


我认为文章是正确的,想在我的

PC上快速测试它。
This is what bugged me :(

http://www.ibm.com/developerworks/ai...ry/au-endianc/

I thought the article was correct and wanted to quickly test it on my
PC.



嗯,它是不是一个很好的解释,但也不是完全错误

。你错过的主要部分是你不需要担心

除非你的程序出口 ;多字节值。绝大多数的
C程序都可以完全移植,而无需担心硬件的字节顺序。


这并不奇怪。那篇文章有一节当结束时

影响代码,它有6个段落。其中5个关于它什么时候

*不*影响代码!只有最后一个短段开始解释

什么时候重要。

Well, it is not a good explanation, but it is not exactly wrong
either. The main part you missed is that you don''t need to worry
unless your program "exports" multi-byte values. The vast majority of
C programs can be entirely portable without any need to worry about
the endianness of the hardware.

It is not surprising. That article has a section "When endianness
affects code" which has 6 paragraphs. 5 of these about when it does
*not* affect the code! Only that last short paragraph starts to explain
when it does matter.


>我想我必须编写自己的函数。
>I guess i will have to write my own function(s).



如果您正在编写网络代码(导出

多字节值的最常见原因),那么您可以使用像htons这样的POSIX函数并且

htonl等。如果您没有这些可用,请自行编写或

您需要更多古怪的东西。


-

Ben。

If you are writing network code (the most common reason to export
multi-byte values) then you can use POSIX functions like htons and
htonl etc. Only write your own if you don''t have these available or
you need to something more outlandish.

--
Ben.


Kelly B写道:
Kelly B wrote:

#include< stdio.h>


#define LITTLE_ENDIAN 0

#define BIG_ENDIAN 1


int endian( ){

int i = 1;

char * p =(char *)& i;


if(p [0] == 1)

返回LITTLE_ENDIAN;

其他

返回BIG_ENDIAN;

}
#include<stdio.h>

#define LITTLE_ENDIAN 0
#define BIG_ENDIAN 1

int endian() {
int i = 1;
char *p = (char *)&i;

if (p[0] == 1)
return LITTLE_ENDIAN;
else
return BIG_ENDIAN;
}



... snip ..


还有一件事。从<转换* signed * int的正确方法是什么br />
一个字节序给另一个(更具体地说是从big-endian到s商场

或反之亦然)。我如何保留*符号*位。

交换字节不能是一个选项,除非我可能以某种方式

保留符号并将数字视为unsigned int或我是否再次以
的方式离开?

...snip..

Just one more thing.What is the right way to convert a *signed* int from
one endianness to another ( more specifically from big-endian to small
or vice versa). How do i preserve the *sign* bit.
Swapping the bytes cannot be an option, unless i probably somehow
preserve the sign and treat the number as an unsigned int or am i
way-off again ?


这篇关于恩迪安独立的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆