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

huangapple 未分类评论41阅读模式
英文:

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();
}

huangapple
  • 本文由 发表于 2020年4月4日 05:28:07
  • 转载请务必保留本文链接:https://java.coder-hub.com/61020666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定