简体   繁体   English

2个字节来缩短java

[英]2 bytes to short java

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.我正在从串行端口读取 133 个长度的数据包,最后 2 个字节包含 CRC 值,2 个字节的值我使用 java 制作了单个(我认为很短)。 this what i have done,这是我所做的,

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我该如何解决这个问题,请帮助我,我遇到了麻烦

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.顺便说一下,按位运算会自动将操作数提升到至少一个 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.真的没有“不允许将一个字节移动超过 7 位”的概念和其他似乎正在流传的谣言。

When converting byte values from a stream into numeric values in Java you have to be very careful with sign extension.在 Java 中将字节值从流转换为数值时,必须非常小心符号扩展。 There is a trap with negative numbers (values from (unsigned) 128-255).有一个带有负数的陷阱(值从(无符号)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.我发现在这些情况下最好用括号明确表示。

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.以上产生0xFFCA,这是错误的。 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 |这是因为 b2 是负数(字节类型是有符号的!),这意味着它何时会被转换为 int 类型以进行按位 | operation, it will be left-padded with 0xF!操作,它将被左填充 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);

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 short type can represent values between -32768 to 32767. 53328 cannot be nicely stored in short, use int instead as it allows you to store unsigned value up to ~10 9 So don't downcast the expression to short as it will net you the signed value. short类型可以表示 -32768 到 32767 之间的值。53328 不能很好地存储在 short 中,使用int代替,因为它允许您将无符号值存储到 ~10 9所以不要将表达式向下转换为 short ,因为它会让你净签名值。

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.第一个字节是最高有效字节。

This works for me这对我有用

short temp = ;
result[0] = (byte) ((temp >>> 8) & 0xFF);
result[1] = (byte)((temp) & 0xFF);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM