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();
}
原文地址:https://www.cnblogs.com/yongfengnice/p/15568130.html