在 Java 中将 short 转换为 byte[]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2188660/
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
Convert short to byte[] in Java
提问by Hugh
How can I convert a short
(2 bytes) to a byte array in Java, e.g.
如何short
在 Java 中将(2 个字节)转换为字节数组,例如
short x = 233;
byte[] ret = new byte[2];
...
it should be something like this. But not sure.
它应该是这样的。但不确定。
((0xFF << 8) & x) >> 0;
EDIT:
编辑:
Also you can use:
你也可以使用:
java.nio.ByteOrder.nativeOrder();
To discover to get whether the native bit order is big or small. In addition the following code is taken from java.io.Bits
which does:
发现获取原生位序是大还是小。此外,以下代码取自于java.io.Bits
:
- byte (array/offset) to boolean
- byte array to char
- byte array to short
- byte array to int
- byte array to float
- byte array to long
- byte array to double
- 字节(数组/偏移量)到布尔值
- 字节数组到字符
- 字节数组缩短
- 字节数组到 int
- 要浮动的字节数组
- 字节数组到长
- 字节数组加倍
And visa versa.
反之亦然。
采纳答案by Alexander Gessler
ret[0] = (byte)(x & 0xff);
ret[1] = (byte)((x >> 8) & 0xff);
回答by Hugh
Figured it out, its:
想通了,它的:
public static byte[] toBytes(short s) {
return new byte[]{(byte)(s & 0x00FF),(byte)((s & 0xFF00)>>8)};
}
回答by abc
It depends how you want to represent it:
这取决于您想如何表示它:
big endian or little endian? That will determine which order you put the bytes in.
Do you want to use 2's complement or some other way of representing a negative number? You should use a scheme that has the same range as the short in java to have a 1-to-1 mapping.
大端还是小端?这将决定您将字节放入的顺序。
您想使用 2 的补码还是其他表示负数的方式?您应该使用与 java 中的 short 具有相同范围的方案来进行 1 对 1 映射。
For big endian, the transformation should be along the lines of: ret[0] = x/256; ret[1] = x%256;
对于大端,转换应该是这样的: ret[0] = x/256; ret[1] = x%256;
回答by Gili
A cleaner, albeit far less efficient solution is:
一个更清洁但效率低得多的解决方案是:
ByteBuffer buffer = ByteBuffer.allocate(2);
buffer.putShort(value);
return buffer.array();
Keep this in mind when you have to do more complex byte transformations in the future. ByteBuffers are very powerful.
当您将来必须进行更复杂的字节转换时,请记住这一点。ByteBuffers 非常强大。
回答by David
An alternative that is more efficient:
一种更有效的替代方法:
// Little Endian
ret[0] = (byte) x;
ret[1] = (byte) (x >> 8);
// Big Endian
ret[0] = (byte) (x >> 8);
ret[1] = (byte) x;
回答by Scott Izu
public short bytesToShort(byte[] bytes) {
return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getShort();
}
public byte[] shortToBytes(short value) {
byte[] returnByteArray = new byte[2];
returnByteArray[0] = (byte) (value & 0xff);
returnByteArray[1] = (byte) ((value >>> 8) & 0xff);
return returnByteArray;
}
回答by bvdb
Several methods have been mentioned here. But which one is the best? Here follows some proof that the following 3 approaches result in the same output for all values of a short
这里提到了几种方法。但哪一个是最好的?下面是一些证明,以下 3 种方法对 a 的所有值产生相同的输出short
// loops through all the values of a Short
short i = Short.MIN_VALUE;
do
{
// method 1: A SIMPLE SHIFT
byte a1 = (byte) (i >> 8);
byte a2 = (byte) i;
// method 2: AN UNSIGNED SHIFT
byte b1 = (byte) (i >>> 8);
byte b2 = (byte) i;
// method 3: SHIFT AND MASK
byte c1 = (byte) (i >> 8 & 0xFF);
byte c2 = (byte) (i & 0xFF);
if (a1 != b1 || a1 != c1 ||
a2 != b2 || a2 != c2)
{
// this point is never reached !!
}
} while (i++ != Short.MAX_VALUE);
Conclusion: less is more ?
结论:少即是多?
byte b1 = (byte) (s >> 8);
byte b2 = (byte) s;
(As other answers have mentioned, watch out for LE/BE).
(正如其他答案所提到的,请注意LE/BE)。
回答by chitrendra chaudhary
short to byte
短到字节
short x=17000;
byte res[]=new byte[2];
res[i]= (byte)(((short)(x>>7)) & ((short)0x7f) | 0x80 );
res[i+1]= (byte)((x & ((short)0x7f)));
byte to short
字节到短
short x=(short)(128*((byte)(res[i] &(byte)0x7f))+res[i+1]);
回答by Serg Burlaka
Short to bytes convert method In Kotlin works for me:
短字节转换方法在 Kotlin 中对我有用:
fun toBytes(s: Short): ByteArray {
return byteArrayOf((s.toInt() and 0x00FF).toByte(), ((s.toInt() and 0xFF00) shr (8)).toByte())
}