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();
}
上一篇:Python 字符串前面加u,r,b的含义


下一篇:Python 了解 bytes 与 str 的区别