如何将Long转换为byte []并返回java如何将长转换为字节[]并返回Java?我正在尝试将long转换为byte [],以便我能够通过tcp连接发送byte []。另一方面,我想取该字节[]并将其转换回double。任何提示将不胜感激。
3 回答

白衣非少年
TA贡献1155条经验 获得超0个赞
我测试了ByteBuffer方法对普通的按位操作,但后者明显更快。
public static byte[] longToBytes(long l) { byte[] result = new byte[8]; for (int i = 7; i >= 0; i--) { result[i] = (byte)(l & 0xFF); l >>= 8; } return result;}public static long bytesToLong(byte[] b) { long result = 0; for (int i = 0; i < 8; i++) { result <<= 8; result |= (b[i] & 0xFF); } return result;}
添加回答
举报
0/150
提交
取消