java+字节转数据 |
您所在的位置:网站首页 › 字节转数字 › java+字节转数据 |
可以使用Buffer作为java.nio软件包一部分提供的来执行转换。 在此,源byte[]数组的长度为8,这是与long值相对应的大小。 首先,将byte[]数组包装在中ByteBuffer,然后ByteBuffer.getLong调用方法以获取long值: ByteBuffer bb = ByteBuffer.wrap(new byte[] {0, 0, 0, 0, 0, 0, 0, 4}); long l = bb.getLong(); System.out.println(l); 结果 4 我要感谢dfa指出了ByteBuffer.getLong注释中的方法。 尽管在这种情况下可能不适用,但是Buffer通过查看具有多个值的数组可以带来s 的魅力。 例如,如果我们有一个8字节的数组,并且希望将其视为两个int值,则可以将该byte[]数组包装为ByteBuffer,将其视为,IntBuffer然后通过IntBuffer.get以下方式获取值: ByteBuffer bb = ByteBuffer.wrap(new byte[] {0, 0, 0, 1, 0, 0, 0, 4}); IntBuffer ib = bb.asIntBuffer(); int i0 = ib.get(0); int i1 = ib.get(1); System.out.println(i0); System.out.println(i1); 结果: 1 4 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |