简体   繁体   English

在字节缓冲区中将字节转换为整数

[英]converting bytes to integers in a bytebuffer

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.我发现我在下面发布的这段代码应该将字节转换为整数,但是我不完全理解这段代码在 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.我相信 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)
    };
public static int bytesToInt(byte[] bytes) {
    return ByteBuffer.wrap(bytes).getInt();
}

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

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