字节排序和数组访问 [英] Byte ordering and array access

查看:84
本文介绍了字节排序和数组访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我听到了不同意见,想要一次性确定

的答案。如果我有一个4个1字节值的数组

,其中索引0是4字节值中最不重要的字节。我可以使用

算术移位运算符来隐藏

底层处理器的字节序,当组装一个原生的4字节值时,例如

如下:


unsigned int integerValue;

unsigned char byteArray [4];


/ * byteArray已填充其他地方,索引0中最不重要的字节,

保证* /


integerValue =(unsigned int)byteArray [0] |

((unsigned int)byteArray [1]<< 8)|

((unsigned int)byteArray [2]<< 16)|

( (unsigned int)byteArray [3]<<<<<<<< 24};


因此,如果byteArray [0]为0x78,则byteArray [1]为0x56,byteArray [2]为

0x34和byteArray [3]是0x12然后integerValue是0x12345678否

处理器的字节顺序?


谢谢提前,


Ben

解决方案

Benjamin M. Stocks写道:

各位大家好,我听到了不同的意见,希望能够一劳永逸地回答这个问题。如果我有一个4个1字节值的数组
其中索引0是4字节值的最不重要的字节。我可以使用算术移位运算符来隐藏底层处理器的字节序,当组装一个原生的4字节值时,如下所示:

unsigned int integerValue;
unsigned char byteArray [4];

/ * byteArray填充在别处,索引0中最不重要的字节,
保证* /

integerValue =(unsigned int)byteArray [0] |
((unsigned int)byteArray [1]<< 8)|
((unsigned int)byteArray [2]<< 16) |(/ unsigned int)byteArray [3]<< 24);

因此,如果byteArray [0]为0x78,则byteArray [1]为0x56,byteArray [2]为
0x34和byteArray [3]是0x12然后integerValue是0x12345678没有
处理器的字节顺序?




是的,你可以做到这一点。转换操作的标准状态定义为/ b $ b,就你正在转移的东西的/值/而言,而不是它的位数

表示。如果你愿意,你可以考虑乘法和

除法的变化。


-

BR,Vladimir





Benjamin M. Stocks写于02/08/06 10:39,:

大家好,
我听到了不同的意见,并希望一劳永逸地回答这个问题。如果我有一个4个1字节值的数组
其中索引0是4字节值的最不重要的字节。我可以使用算术移位运算符来隐藏底层处理器的字节序,当组装一个原生的4字节值时,如下所示:

unsigned int integerValue;
unsigned char byteArray [4];

/ * byteArray填充在别处,索引0中最不重要的字节,
保证* /

integerValue =(unsigned int)byteArray [0] |
((unsigned int)byteArray [1]<< 8)|
((unsigned int)byteArray [2]<< 16) |(/ unsigned int)byteArray [3]<< 24);

因此,如果byteArray [0]为0x78,则byteArray [1]为0x56,byteArray [2]为
0x34和byteArray [3]是0x12然后integerValue是0x12345678没有
处理器的字节顺序?




是的,这个很好。好吧,差不多:我可以看到两个

潜在的可移植性问题:


- C保证char至少有8位,

但允许它有更多。取决于

你的意思是1字节值,您可能需要

来替换CHAR_BIT的8,16,24,2 * CHAR_BIT和

3 * CHAR_BIT(CHAR_BIT宏位于< limits.h>) 。


- C保证int至少有16位,

但不保证它有多达32

(或4 * CHAR_BIT)。也就是说,int可能太窄了

来保存四个字节。这可能会在两个方面弄乱你的东西:首先,你明显使用一个两磅重的袋子

来保持四磅......好吧,更好的未说明。 />
其次,如果

移位距离严格小于移位值中的

位数,则仅保证移位有效,所以如果int是16位

宽16位和24位位移都是未定义的。


-
Er ********* @ sun.com


< blockquote>

" Benjamin M. Stocks" < ST ***** @ ieee.org>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...

大家好,


在这里忽略你的问题:

unsigned int integerValue;
unsigned char byteArray [4];

/ * byteArray填充在别处,索引0中最不重要的字节,
保证* /

integerValue =(unsigned int)byteArray [0] |
( (unsigned int)byteArray [1]<< 8)|
((unsigned int)byteArray [2]<< 16)|
((unsigned int)byteArray [3]< < 24);




您可能需要或想要声明''integerValue''作为更明确的

类型: ''unsigned long''或''uint32_t''。

Rod Pemberton


Hello all,
I''ve heard differing opinions on this and would like a definitive
answer on this once and for all. If I have an array of 4 1-byte values
where index 0 is the least signficant byte of a 4-byte value. Can I use
the arithmatic shift operators to hide the endian-ness of the
underlying processor when assembling a native 4-byte value like
follows:

unsigned int integerValue;
unsigned char byteArray[4];

/* byteArray is populated elsewhere, least signficant byte in index 0,
guaranteed */

integerValue = (unsigned int)byteArray[0] |
((unsigned int)byteArray[1] << 8) |
((unsigned int)byteArray[2] << 16) |
((unsigned int)byteArray[3] << 24);

So if byteArray[0] was 0x78, byteArray[1] was 0x56, byteArray[2] was
0x34 and byteArray[3] was 0x12 then would integerValue be 0x12345678 no
matter the endian-ness of the processor?

Thanks in advance,

Ben

解决方案

Benjamin M. Stocks wrote:

Hello all,
I''ve heard differing opinions on this and would like a definitive
answer on this once and for all. If I have an array of 4 1-byte values
where index 0 is the least signficant byte of a 4-byte value. Can I use
the arithmatic shift operators to hide the endian-ness of the
underlying processor when assembling a native 4-byte value like
follows:

unsigned int integerValue;
unsigned char byteArray[4];

/* byteArray is populated elsewhere, least signficant byte in index 0,
guaranteed */

integerValue = (unsigned int)byteArray[0] |
((unsigned int)byteArray[1] << 8) |
((unsigned int)byteArray[2] << 16) |
((unsigned int)byteArray[3] << 24);

So if byteArray[0] was 0x78, byteArray[1] was 0x56, byteArray[2] was
0x34 and byteArray[3] was 0x12 then would integerValue be 0x12345678 no
matter the endian-ness of the processor?



Yes, you can do this. Standard states that shift operations are defined
in terms of the /value/ of the thing you''re shifting, not it''s bit
representation. You can think of shifts in terms of multiplication and
division, if you will.

--
BR, Vladimir




Benjamin M. Stocks wrote On 02/08/06 10:39,:

Hello all,
I''ve heard differing opinions on this and would like a definitive
answer on this once and for all. If I have an array of 4 1-byte values
where index 0 is the least signficant byte of a 4-byte value. Can I use
the arithmatic shift operators to hide the endian-ness of the
underlying processor when assembling a native 4-byte value like
follows:

unsigned int integerValue;
unsigned char byteArray[4];

/* byteArray is populated elsewhere, least signficant byte in index 0,
guaranteed */

integerValue = (unsigned int)byteArray[0] |
((unsigned int)byteArray[1] << 8) |
((unsigned int)byteArray[2] << 16) |
((unsigned int)byteArray[3] << 24);

So if byteArray[0] was 0x78, byteArray[1] was 0x56, byteArray[2] was
0x34 and byteArray[3] was 0x12 then would integerValue be 0x12345678 no
matter the endian-ness of the processor?



Yes, this is fine. Well, almost: I can see two
potential portability problems:

- C guarantees that a char has at least eight bits,
but permits it to have more. Depending on just
what you mean by "1-byte values," you might want
to replace 8,16,24 by CHAR_BIT, 2*CHAR_BIT, and
3*CHAR_BIT (the CHAR_BIT macro is in <limits.h>).

- C guarantees that an int has at least sixteen bits,
but does not promise that it has as many as 32
(or 4*CHAR_BIT). That is, an int may be too narrow
to hold four bytes. This could mess things up in
two ways: first, you obviously use a two-pound sack
to hold four pounds of ... well, better unsaid.
Second, shifts are only guaranteed to work if the
shift distance is strictly less than the number of
bits in the value shifted, so if int is sixteen bits
wide both the 16- and 24-bit shifts are undefined.

--
Er*********@sun.com



"Benjamin M. Stocks" <st*****@ieee.org> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

Hello all,
Ignoring your question here:
unsigned int integerValue;
unsigned char byteArray[4];

/* byteArray is populated elsewhere, least signficant byte in index 0,
guaranteed */

integerValue = (unsigned int)byteArray[0] |
((unsigned int)byteArray[1] << 8) |
((unsigned int)byteArray[2] << 16) |
((unsigned int)byteArray[3] << 24);



You''ll probably need or want to declare ''integerValue'' as a more definitive
type: ''unsigned long'' or ''uint32_t''.
Rod Pemberton


这篇关于字节排序和数组访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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