其他分享
首页 > 其他分享> > android开发通过ByteBuffer实现基本数据类型转换

android开发通过ByteBuffer实现基本数据类型转换

作者:互联网

public static long bytesToLong(byte[] bytes) {
    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.put(bytes, 0, bytes.length);
    buffer.flip();
    return buffer.getLong();
}

public static int bytesToInt(byte[] bytes) {
    ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.put(bytes, 0, bytes.length);
    buffer.flip();
    return buffer.getInt();
}

public static byte[] longToBytes(long num) {
    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.putLong(num);
    buffer.flip();
    return buffer.array();
}

标签:类型转换,buffer,bytes,static,allocate,ByteBuffer,android,public
来源: https://www.cnblogs.com/yongfengnice/p/15568130.html