字节到LONG [英] byte to LONG
问题描述
我逐字节收到
0x8E byte1
0x3D byte2
0x64 byte3
0x5F byte4
如何构造为LONG 0x645F8E3D(向左移动>> 16)
谢谢。
Magix写道:我逐字节收到
0x8E byte1
0x3D byte2
0x64 byte3
0x5F byte4
如何构造为LONG 0x645F8E3D(向左移动>> 16)>
确定每个应该去哪里,然后将它移到那里然后
按位或长一点。
长result = 0;
unsigned char byte2 = 0x3D;
result | = byte2<< 8;
你可以从那里弄清楚剩下的。
(我假设无符号字符是8位在上面的
和那个长度至少是unsigned char的两倍大。不是
问题你可以使用CHAR_BIT代替8,结果| = byte2<<< CHAR_BIT;)
-
托马斯。
Thomas Stegen写道:
Magix写道:我逐字节地收到了
0x8E byte1
0x3D byte2
0x64 byte3
0x5F byte4
如何构造为LONG 0x645F8E3D(向左移动>> 16)
确定每个应该去哪里,然后将它移到那里然后按位或长按。
long result = 0;
unsigned char byte2 = 0x3D;
结果| = byte2<< 8;
你可以从那里弄清楚其余部分。
(我已经假设上面的无符号字符是8位
这个长度至少是无符号字符的两倍。
我不认为你做了那个假设。
一个可移植的表达式,使得长的无符号值
0x645F8E3D,在这些字节值之外,是:
(((long unsigned)byte3< ;< 24)
+((长无符号)byte4<< 16)
+((无符号)byte1<< 8)
+ byte2)
CHAR_BIT不是它的一部分。
-
pete
2004年10月9日星期六02:22:25 +0000,pete写道:
一个可移植的表达式一个长的无符号值
怎么样sizeof(long)< 4?或endianess?
I received byte by byte
0x8E byte1
0x3D byte2
0x64 byte3
0x5F byte4
How can I construct to be LONG 0x645F8E3D (shift left >> 16)
Thanks.解决方案Magix wrote:I received byte by byte
0x8E byte1
0x3D byte2
0x64 byte3
0x5F byte4
How can I construct to be LONG 0x645F8E3D (shift left >> 16)
Determine where each by should go and then shift it there and then
bitwise or it with a long.
long result = 0;
unsigned char byte2 = 0x3D;
result |= byte2 << 8;
You can figure out the rest from there I guess.
(I have made the assumption that an unsigned char is 8 bits in the above
and that long is at least twice as large an unsigned char. Not
unreasonable, but not guaranteed. To get around the bytes are not 8 bits
problem you can use CHAR_BIT instead of 8, result |= byte2 << CHAR_BIT;)
--
Thomas.
Thomas Stegen wrote:
Magix wrote:I received byte by byte
0x8E byte1
0x3D byte2
0x64 byte3
0x5F byte4
How can I construct to be LONG 0x645F8E3D (shift left >> 16)
Determine where each by should go and then shift it there and then
bitwise or it with a long.
long result = 0;
unsigned char byte2 = 0x3D;
result |= byte2 << 8;
You can figure out the rest from there I guess.
(I have made the assumption that an unsigned char is 8 bits
in the above
and that long is at least twice as large an unsigned char.
I don''t think that you made that assumption.
A portable expression to make a long unsigned value of
0x645F8E3D, out of those byte values, is:
(((long unsigned)byte3 << 24)
+ ((long unsigned)byte4 << 16)
+ (( unsigned)byte1 << 8)
+ byte2)
CHAR_BIT isn''t part of it.
--
pete
On Sat, 09 Oct 2004 02:22:25 +0000, pete wrote:
A portable expression to make a long unsigned value of
What about where sizeof(long) < 4 ? Or endianess ?
这篇关于字节到LONG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!