在 C# 中将 2 个字节 (HighByte/LowByte) 组合成一个带符号的 Int
Combining 2 Bytes (HighByte/LowByte) to a signed Int in C#
在过去的 3 个小时里,我一直在为此苦苦挣扎,但我做错了。
我有 highByte/lowByte 值,我需要将它们组合成一个名为 temp 的有符号整数(我认为)。我知道输入和所需的输出,但此时我认为这可能完全偏离轨道。
int temp = ((highByte) & 0xFF) << 8 | (lowByte) & 0xFF;
我正在使用这个函数,但它没有返回所需的输出。
一定是这个转换有问题,或者我应用到这个问题上的逻辑。期望的结果是:
If both highByte/lowByte bytes have the value 255, the output should
be -1.
int
是 4 字节整数值(其系统名称 Int32)。如果要将 highByte 的高字节视为符号,请将表达式的右部分转换为 short
(Int16)
int temp = (short)(((highByte) & 0xFF) << 8 | (lowByte) & 0xFF);
在过去的 3 个小时里,我一直在为此苦苦挣扎,但我做错了。 我有 highByte/lowByte 值,我需要将它们组合成一个名为 temp 的有符号整数(我认为)。我知道输入和所需的输出,但此时我认为这可能完全偏离轨道。
int temp = ((highByte) & 0xFF) << 8 | (lowByte) & 0xFF;
我正在使用这个函数,但它没有返回所需的输出。
一定是这个转换有问题,或者我应用到这个问题上的逻辑。期望的结果是:
If both highByte/lowByte bytes have the value 255, the output should be -1.
int
是 4 字节整数值(其系统名称 Int32)。如果要将 highByte 的高字节视为符号,请将表达式的右部分转换为 short
(Int16)
int temp = (short)(((highByte) & 0xFF) << 8 | (lowByte) & 0xFF);