java java将int转换为short
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2485643/
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
java converting int to short
提问by changed
I am calculating 16 bit checksum on my data which i need to send to server where it has to recalculate and match with the provided checksum. Checksum value that i am getting is in int but i have only 2 bytes for sending the value.So i am casting intto shortwhile calling shortToBytesmethod. This works fine till checksum value is less than 32767 thereafter i am getting negative values.
我正在计算我的数据的 16 位校验和,我需要将其发送到服务器,在那里它必须重新计算并与提供的校验和匹配。校验值我得到是INT,但我有用于发送value.So我正在铸造只有2个字节int,以short同时调用shortToBytes方法。这工作正常,直到校验和值小于 32767 之后我得到负值。
Thing is java does not have unsigned primitives, so i am not able to send values greater than max value of signed shortallowed.
事情是java没有无符号原语,所以我无法发送大于short允许的有符号最大值的值。
How can i do this, converting int to short and send over the network without worrying about truncation and signed & unsigned int.
我该如何做到这一点,将 int 转换为 short 并通过网络发送,而不必担心截断以及有符号和无符号 int。
Also on both the side i have java program running.
同样在双方我都有java程序运行。
private byte[] shortToBytes(short sh) {
byte[] baValue = new byte[2];
ByteBuffer buf = ByteBuffer.wrap(baValue);
return buf.putShort(sh).array();
}
private short bytesToShort(byte[] buf, int offset) {
byte[] baValue = new byte[2];
System.arraycopy(buf, offset, baValue, 0, 2);
return ByteBuffer.wrap(baValue).getShort();
}
回答by Stephen C
Firstly, Java int, shortand bytetypes are all signed not unsigned. Secondly, when you cast a Java intto a short, etc you will get silent truncation.
首先,Java的int,short和byte类型都签署不无符号。其次,当您将 Java 转换int为 ashort等时,您将获得无声截断。
Whether this matters depends on the nature of the checksum algorithm. If it is a simple sum, or a bitwise algorithm there is a good chance that the algorithm is just fine when implemented using Java signed integers. For example, those "negative" 16bit checksums could be correct when interpreted by something expecting unsigned values.
这是否重要取决于校验和算法的性质。如果它是一个简单的总和或按位算法,那么当使用 Java 有符号整数实现时,该算法很可能很好。例如,那些“负”的 16 位校验和在被期望无符号值的东西解释时可能是正确的。
On the other hand, the semantic of multiplication and division are such that signed and unsigned flavors have to be handled separately. (At least, that's what I infer from the unscientific approach of looking at the x86 instruction set ... which has separate instructions for signed versus unsigned multiplication and division.)
另一方面,乘法和除法的语义使得有符号和无符号风味必须分开处理。(至少,这是我从查看 x86 指令集的不科学方法中推断出来的......它具有单独的有符号与无符号乘法和除法指令。)
EDITI understand that you are calculating CRC-16. Since that can be computed by shifting and XORing, there should be no concerns about signed versus unsigned numbers during the calculation.
编辑我知道您正在计算 CRC-16。由于可以通过移位和异或运算来计算,因此在计算过程中不应该担心有符号数与无符号数。
In short, you don't have anything to worry about.
简而言之,您无需担心。
回答by cletus
charis an unsigned 16 bit type. In fact it's the only unsigned type in Java. You can use it for calculating the checksum and then use a ByteBufferto get the bytes or simply use bitwise and and right shifting to get the bytes.
char是无符号的 16 位类型。事实上,它是 Java 中唯一的无符号类型。您可以使用它来计算校验和,然后使用 aByteBuffer来获取字节或简单地使用按位和右移来获取字节。
Bear in mind that bytes are signed.
请记住,bytes 是有符号的。
回答by VolatileDream
You are still getting the same bit value as the server. So, to see the right numerical value replace the ByteBuffer.wrap(baValue).getShort()to a ByteBuffer.wrap(baValue).getInt(). This should give you the same numerical value as the server.
您仍然获得与服务器相同的位值。因此,要查看正确的数值,请将 替换ByteBuffer.wrap(baValue).getShort()为 a ByteBuffer.wrap(baValue).getInt()。这应该为您提供与服务器相同的数值。
回答by Michael Aaron Safyan
When you say that you are getting negative values, I assume you mean when you read the 16 bit value and convert it to an integer. The reason for this is that sign extension causes the most significant bit (which is a 1) to be replicated when the shortis widened to an int. The simple workaround is to bitwise-and the reconstructed integer with 0xFFFF, which will ensure that only the least signficant 16 bits are non-zero.
当你说你得到负值时,我假设你的意思是当你读取 16 位值并将其转换为整数时。这样做的原因是符号扩展导致最高有效位(即 1)在short被扩展为时被复制int。简单的解决方法是按位和重建整数0xFFFF,这将确保只有最低有效的 16 位是非零的。

