Endianess:为什么这个代码不能改变BE机器上的价值? [英] Endianess: why does this code not change value on BE machine?

查看:59
本文介绍了Endianess:为什么这个代码不能改变BE机器上的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,


我在这个群体中搜索过Big / Little endian问题,不要杀了我,

我知道endianess问题已经讨论了1000次。但我的

问题有点不同:


我已经多次看到了这个函数,它转换了存储的数据

将Big Endian(BE)格式转换为主机原生格式(LE机器上的LE,BE机器上的
BE):


/ * - 此代码交换Little Endian机器上的字节

- 此代码返回Big Endian机器上未修改的''数据'。

* /

静态short getShortBE(char * data)

{

return(short)((data [0]<< 8)| data [1]);

}


我能理解为什么这个函数在LE机器上交换字节,但

为什么它不会改变''数据在BE机器上?我试图用图表和一切来理解它的价值,但是我的大脑疯了!可以

有人给我一个简单的解释吗? :)我真的很好奇...


一个函数怎么会看起来像是对立:交换

字节是机器,不要在LE机器上更改值(即,

读取和转换LE数据)? :)希望我能回答第二个问题

当我理解第一个问题时... :)

这里是完整的测试程序:


#include< stdlib.h>

#include< stdio.h>


/ *这个函数很有用当读取和转换以大端格式存储

的数据时



- 此代码在Little Endian机器上交换字节

- 此代码在Big Endian机器上返回未修改的''data'。

* /

静态短getShortBE(char * data)

{

return(短)((data [0]<< 8)| data [1]);

}


int main(int argc,char ** argv)

{

int a;

char c ;

短数据1;

短数据2;

char * d;


(无效) argc;

(无效)argv;


a = 0x01020304;

c =((char *)& a)[0 ];


if(c == 1)

{

fprintf(stdout,"整数a:%x - 第一个字节:%x(MSB) - > Big

endian machine.\ n,a,c);

}

else if(c == 4)

{

fprintf(stdout,"整数a:%x - 第一个字节:%x(LSB) - > Little

endian machine.\\ \\ n",a,c);

}

else

{

fprintf(stdout,"整数a:%x - 第一个字节:%x - >一个怪人

machine.\\\
,a,c);

}


data1 = 0x0102;

d =((char *)& data1);


fprintf(stdout," Data [ 0]:%x - 数据[1]:%x \ n",

* d,*(d + 1));


data2 = getShortBE((char *)& data1);


d =((char *)& data2);

fprintf(stdout," After GET:数据[0]:%x - 数据[1]:%x \ n",

* d,*(d + 1));


返回1;

}


输出:

-------


在Sun上:

Ok,

I''ve searched this group for Big/Little endian issues, don''t kill me,
I know endianess issues have been discussed a 1000 times. But my
question is a bit different:

I''ve seen the follwing function several times, it converts data stored
in Big Endian (BE) format into host native format (LE on LE machines,
BE on BE machines):

/* - this code swaps the bytes on a Little Endian Machine
- this code returns ''data'' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}

I can understand why this function swaps bytes on a LE machine, but
why doesn''t it alter ''data'' on a BE machine? I''ve tried to understand
it with diagrams and everything, but my brain went just crazy! Can
anyone give me a simple explanation please? :) I''m really curious...

And how would a function look like which does the "opposite": swap
bytes on a BE machine, don''t change values on a LE machine (that is,
"read and convert LE data")? :) Hope I can answer the 2nd quesion
myself when I understand the 1st question... :)
Here''s the complete test program:

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

/* This function is useful when reading and converting data which is
stored
in Big Endian Format.
- this code swaps the bytes on a Little Endian Machine
- this code returns ''data'' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}

int main (int argc, char **argv)
{
int a;
char c;
short data1;
short data2;
char *d;

(void)argc;
(void)argv;

a = 0x01020304;
c = ((char *)&a)[0];

if (c == 1)
{
fprintf (stdout, "Integer a: %x - first byte: %x (MSB) -> Big
endian machine.\n", a, c);
}
else if (c == 4)
{
fprintf (stdout, "Integer a: %x - first byte: %x (LSB) -> Little
endian machine.\n", a, c);
}
else
{
fprintf (stdout, "Integer a: %x - first byte: %x -> A weirdo
machine.\n", a, c);
}

data1 = 0x0102;
d = ((char *)&data1);

fprintf (stdout, "Data[0]: %x - Data[1]: %x\n",
*d, *(d + 1));

data2 = getShortBE ((char *)&data1);

d = ((char *)&data2);
fprintf (stdout, "After GET: Data[0]: %x - Data[1]: %x\n",
*d, *(d + 1));

return 1;
}

Output:
-------

On Sun:

./Endian



In teger a:1020304 - 第一个字节:1(MSB) - > Big endian机器。

数据[0]:1 - 数据[1]:2

GET后:数据[0]:1 - 数据[1]:2


在Linux(i386)上:


整数a:1020304 - 第一个字节:4(LSB) - >小端机。

数据[0]:2 - 数据[1]:1

GET后:数据[0]:1 - 数据[1]:2


谢谢,Oliver


Integer a: 1020304 - first byte: 1 (MSB) -> Big endian machine.
Data[0]: 1 - Data[1]: 2
After GET: Data[0]: 1 - Data[1]: 2

On Linux (i386):

Integer a: 1020304 - first byte: 4 (LSB) -> Little endian machine.
Data[0]: 2 - Data[1]: 1
After GET: Data[0]: 1 - Data[1]: 2

Thanks, Oliver

推荐答案



" Oliver Knoll" < TK **** @ bluewin.ch>写了

"Oliver Knoll" <tk****@bluewin.ch> wrote

/ * - 这段代码在Little Endian机器上交换字节
- 此代码在Big Endian机器上返回数据未修改。
* /
静态短getShortBE(字符*数据)
{返回(短)((数据[0]<< 8)|数据[1]);
我理解为什么这个函数在LE机器上交换字节,但是为什么它不会改变BE机器上的''数据'?我试图用图表和一切来理解它,但我的大脑疯了!可以吗?有人给我一个简单的解释吗? :)我真的好奇......

首先任意数据应该是unsigned char。普通字符是实际的

文本。


不要被<<<<<运营商。这表明数据正在转移

向左在内存中,但实际上它总是将不太重要的位移动到更重要的位置。

因此,您的函数采用任意字节流,并处理

第一个作为前8位,第二个作为低8位

的16位数。

顺便说一下它不会起作用预计如果CHAR_BIT不是8,那么它不是b $ b并不总是如此。 short也不一定是十六位。

一个函数如何看起来像BE机器上的反向:交换
字节,不会改变LE机器上的值(即,读取和转换LE数据)? :)希望我能解答第二个问题
当我理解第一个问题时...... :)

所以你可以简单地通过
将你的任意比特流视为小端
将第一个字节放在最不重要的位置,然后将第二个字节向上移动到最重要的位置。

/* - this code swaps the bytes on a Little Endian Machine
- this code returns ''data'' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}

I can understand why this function swaps bytes on a LE machine, but
why doesn''t it alter ''data'' on a BE machine? I''ve tried to understand
it with diagrams and everything, but my brain went just crazy! Can
anyone give me a simple explanation please? :) I''m really curious...
Firstly arbitrary data should be unsigned char. Plain char is for actual
text.

Don''t be fooled by the << operator. This suggests that data is being shifted
"leftwards" in memory, but in fact it always moves less significant bits to
the more significant position.
Your function therefore takes an arbitrary stream of bytes, and treats the
first one as the top eight bits and the second one as the lower eight bits
of a 16-bit number.
Incidentally it will not work as expected if CHAR_BIT is not eight, which it
isn not always. short isn''t necessarily sixteen bits, either.
And how would a function look like which does the "opposite": swap
bytes on a BE machine, don''t change values on a LE machine (that is,
"read and convert LE data")? :) Hope I can answer the 2nd quesion
myself when I understand the 1st question... :)
So you can treat your arbitrary bit stream as little endian simply by
placing the first byte in the least-significant position, and shifting up
the second byte to the most significant position.


在文章< b2 ************************** @ posting.google.com>,

Oliver Knoll< tk **** @ bluewin.ch>写道:
In article <b2**************************@posting.google.com >,
Oliver Knoll <tk****@bluewin.ch> wrote:
static short getShortBE(char * data)
{返回(短)((data [0]<< 8)| data [ 1]);
}

我能理解为什么这个函数在LE机器上交换字节,但是为什么它不会改变BE上的''数据''机?我试图用图表和一切来理解它,但我的大脑疯了!可以吗?有人给我一个简单的解释吗? :)我真的很好奇......
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}

I can understand why this function swaps bytes on a LE machine, but
why doesn''t it alter ''data'' on a BE machine? I''ve tried to understand
it with diagrams and everything, but my brain went just crazy! Can
anyone give me a simple explanation please? :) I''m really curious...




嗯,有一种方法可以看出这个函数并不依赖于

endianness,所以它会在big-or / $
小端机器上得到相同的结果。但是只需将数据作为简短信息获取将会给出两个不同的结果。


- Richard



Well, one way to see it is that this function doesn''t depend on
endianness at all, so it will give the same result on a big- or
little-endian machine. But just accesing the data as a short will
give different results on the two.

-- Richard

tk****@bluewin.ch (Oliver Knoll)写道:
tk****@bluewin.ch (Oliver Knoll) wrote:
/ * - 此代码在Little Endian机器上交换字节
- 此代码在Big Endian机器上返回未修改的''数据'。
* /
静态短getShortBE(char *数据)
{
返回(短)
^^^^^^无用演员((data [0]<< 8)| data [1]);


由于符号扩展而实现定义的行为。你应该使用无符号数据类型进行位操作,例如:


short getShortBE(void * data)

{

unsigned char * p = data;

return(p [0]<< CHAR_BIT)| p [1]; }
我能理解为什么这个函数在LE机器上交换字节,但是为什么它不会改变BE机器上的''数据'?


用你自己的话解释为什么它适用于LE机器,然后

然后很明显为什么它在BE上什么都不做。

一个函数如何看起来像是BE机器上的反向:交换
字节,不能改变LE机器上的值(即,
"读取和转换LE数据)?
/* - this code swaps the bytes on a Little Endian Machine
- this code returns ''data'' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ^^^^^^ useless cast ((data[0] << 8) | data[1]);
Implementation-defined behaviour, due to sign-extension. You should
use unsigned data types for bit manipuation, eg:

short getShortBE(void *data)
{
unsigned char *p = data;
return ( p[0] << CHAR_BIT ) | p[1] ; } I can understand why this function swaps bytes on a LE machine, but
why doesn''t it alter ''data'' on a BE machine?
Explain in your own words why it works on an LE machine, and
then it should be obvious why it does nothing on BE.
And how would a function look like which does the "opposite": swap
bytes on a BE machine, don''t change values on a LE machine (that is,
"read and convert LE data")?




用1切换0。



Switch the ''0'' with the ''1''.

这篇关于Endianess:为什么这个代码不能改变BE机器上的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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