英文:
converting bytes to integers in a bytebuffer
问题
public static byte[] intToBytes(int i) {
return new byte[] {
(byte) ((i & 0x000000FF) >> 0),
(byte) ((i & 0x0000FF00) >> 8),
(byte) ((i & 0x00FF0000) >> 16),
(byte) ((i & 0xFF000000) >> 24)
};
}
英文:
I have found this piece of code that I have posted below that should
convert bytes to integers, however I do not fully understand how this piece
of code works in java. I believe bytebuffer is used in this case in a full
piece of code as it is just a part I am struggling to understand.
public static byte[] intToBytes(int i) {
return new byte[] {
(byte) ((i & 0x000000FF) >> 0),
(byte) ((i & 0x0000FF00) >> 8),
(byte) ((i & 0x00FF0000) >> 16),
(byte) ((i & 0xFF000000) >> 24)
};
答案1
得分: 0
public static int bytesToInt(byte[] bytes) {
return ByteBuffer.wrap(bytes).getInt();
}
英文:
public static int bytesToInt(byte[] bytes) {
return ByteBuffer.wrap(bytes).getInt();
}
专注分享java语言的经验与见解,让所有开发者获益!
评论