2个字节来缩短java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/736815/
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-11 18:52:48  来源:igfitidea点击:

2 bytes to short java

javatype-conversionbytecoreshort

提问by

i'm reading 133 length packet from serialport,last 2 bytes contain CRC values,2 bytes value i've make single(short i think) using java. this what i have done,

我正在从串行端口读取 133 个长度的数据包,最后 2 个字节包含 CRC 值,2 个字节的值我使用 java 制作了单个(我认为很短)。这是我所做的,

short high=(-48 & 0x00ff);
short low=80;

short c=(short) ((high<<8)+low);

but i'm not getting correct result,is it problem because signed valued? how can i solve this problem,plz help me i'm in trouble

但我没有得到正确的结果,是不是因为签名值有问题?我该如何解决这个问题,请帮助我,我遇到了麻烦

回答by Neil Coffey

Remember, you don't have to tie yourself in knots with bit shifting if you're not too familiar with the details. You can use a ByteBuffer to help you out:

请记住,如果您不太熟悉细节,则不必将自己束缚在位移位上。您可以使用 ByteBuffer 来帮助您:

ByteBuffer bb = ByteBuffer.allocate(2);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.put(firstByte);
bb.put(secondByte);
short shortVal = bb.getShort(0);

And vice versa, you can put a short, then pull out bytes.

反之亦然,可以先放一个短,然后再拉出字节。

By the way, bitwise operations automatically promote the operands to at least the width of an int. There's really no notion of "not being allowed to shift a byte more than 7 bits" and other rumours that seem to be going round.

顺便说一下,按位运算会自动将操作数提升到至少一个 int 的宽度。真的没有“不允许将字节移动超过 7 位”的概念以及其他似乎正在流传的谣言。

回答by Lawrence Dol

When converting byte values from a stream into numeric values in Java you have to be very careful with sign extension. There is a trap with negative numbers (values from (unsigned) 128-255).

在 Java 中将字节值从流转换为数值时,必须非常小心符号扩展。有一个带有负数的陷阱(值从(无符号)128-255)。

Try this (it works if hi and lo are any Java integer type) :

试试这个(如果 hi 和 lo 是任何 Java 整数类型,它就可以工作):

short val=(short)(((hi & 0xFF) << 8) | (lo & 0xFF));

I find it's best to be explicit with the parentheses in these cases.

我发现在这些情况下最好用括号明确表示。

回答by akarnokd

The other answers are OK, but I would like to put an emphasis on the type:

其他答案都可以,但我想强调一下类型:

short high=(-48 & 0x00ff);
short low=80;

int c= ((high & 0xFF) << 8) | (low & 0xFF);

The shorttype can represent values between -32768 to 32767. 53328 cannot be nicely stored in short, use intinstead as it allows you to store unsigned value up to ~109So don't downcast the expression to short as it will net you the signed value.

short类型可以表示 -32768 到 32767 之间的值。53328 不能很好地存储在 short 中,int而是使用它,因为它允许您将无符号值存储到 ~10 9所以不要将表达式向下转换为 short ,因为它会为您提供有符号价值。

回答by Lake

This happens when trying to concatenate bytes (very subtle)

尝试连接字节时会发生这种情况(非常微妙)

byte b1 = (byte) 0xAD;
byte b2 = (byte) 0xCA;
short s = (short) (b1<<8 | b2);

The above produces 0xFFCA, which is wrong. This is because b2 is negative (byte type is signed!), which means that when it will get converted to int type for the bit-wise | operation, it will be left-padded with 0xF!

以上产生0xFFCA,这是错误的。这是因为 b2 是负数(字节类型是有符号的!),这意味着它何时会被转换为 int 类型以进行按位 | 操作,它将被左填充 0xF!

Therefore, you must remember to mask-out the padded bytes so that they will definitely be zero:

因此,您必须记住屏蔽填充的字节,以便它们肯定为零:

short s = (short) (b1<<8 | b2 & 0xFF);

回答by Mariusz Wiazowski

You can convert 2 bytes to a short in a more readable and elegant way.

您可以以更易读和优雅的方式将 2 个字节转换为 short。

short s = ByteBuffer.wrap(new byte[]{0x01, 0x02}).getShort();
// now s equals 258 = 256 + 2

The first byte is the most significant byte.

第一个字节是最高有效字节。