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();
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM