C++ 如何使用高字节和低字节?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6090561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:29:01  来源:igfitidea点击:

How to use high and low bytes?

c++bytebytearraybit16-bit

提问by Sterling

I am trying to represent 32768 using 2 bytes. For the high byte, do I use the same values as the low byte and it will interpret them differently or do I put the actual values? So would I put something like 32678 0 or 256 0? Or neither of those? Any help is appreciated.

我正在尝试使用 2 个字节来表示 32768。对于高字节,我是否使用与低字节相同的值,它会以不同的方式解释它们还是我放置实际值?那么我会输入 32678 0 或 256 0 之类的东西吗?或者两者都没有?任何帮助表示赞赏。

回答by John McFarlane

In hexadecimal, your number is 0x8000 which is 0x80 and 0x00. To get the low byte from the input, use low=input & 0xffand to get the high byte, use high=(input>>8) & 0xff.

在十六进制中,您的号码是 0x8000,即 0x80 和 0x00。要从输入中获取低字节,请使用low=input & 0xff并获取高字节,请使用high=(input>>8) & 0xff.

Get the input back from the low and high byes like so: input=low | (high<<8).

获得从低输入背部和高轮空,像这样:input=low | (high<<8)

Make sure the integer types you use are big enough to store these numbers. On 16-bit systems, unsigned int/shortor signed/unsigned longshould be be large enough.

确保您使用的整数类型足够大以存储这些数字。在 16 位系统上,unsigned int/shortsigned/unsigned long应该足够大。

回答by Ignacio Vazquez-Abrams

Bytes can only contain values from 0 to 255, inclusive. 32768 is 0x8000, so the high byte is 128 and the low byte is 0.

字节只能包含 0 到 255(含)之间的值。32768 是 0x8000,所以高字节是 128,低字节是 0。

回答by cyberponk

Pointers can do this easily, are MUCH FASTER than shifts and requires no processor math.

指针可以很容易地做到这一点,比移位快得多,并且不需要处理器数学。

Check this answer

检查这个答案

BUT: If I understood your problem, you need up to 32768 stored in 2 bytes, so you need 2 unsigned int's, or 1 unsigned long. Just change int for long and char for int, and you're good to go.

但是:如果我理解你的问题,你最多需要 32768 个存储在 2 个字节中,所以你需要 2 个 unsigned int 或 1 个 unsigned long。只需将 int 更改为 long 并将 char 更改为 int,您就可以开始了。

回答by Ernesto A. Garrote

Try this function. Pass your Hi_Byte and Lo_Byte to the function, it returns the value as Word.

试试这个功能。将您的 Hi_Byte 和 Lo_Byte 传递给该函数,它会将值作为 Word 返回。

WORD MAKE_WORD( const BYTE Byte_hi, const BYTE Byte_lo)
{
     return   (( Byte_hi << 8  ) | Byte_lo & 0x00FF );
}

回答by Alnitak

32768 is 0x8000, so you would put 0x80 (128) in your high byte and 0 in your low byte.

32768 是 0x8000,因此您可以将 0x80 (128) 放在高字节中,将 0 放在低字节中。

That's assuming unsigned values, of course. 32768 isn't actually a legal value for a signed 16-bit value.

当然,这是假设无符号值。32768 实际上不是有符号 16 位值的合法值。

回答by azyoot

32768 in hex is 0080 on a little-endian platform. The "high" (second in our case) byte contains 128, and the "low" one 0.

在小端平台上,十六进制的 32768 是 0080。“高”(在我们的例子中是第二个)字节包含 128,而“低”字节包含 0。