简体   繁体   English

Java如何从字节流中读取64位无符号长

[英]Java how to read a 64 bit unsigned long from a byte stream

I'm trying to read an unsigned long from byte stream (that has been generated by a C program). 我正在尝试从字节流(由C程序生成)读取无符号长整数。 Since the number might be too big for a Java long I imagined the best way to do this would be to read the 8 bytes into a BigInteger (as below) but this doesn't work. 由于数量可能过大了Java的long我想象中要做到这一点是读8个字节转换为BigInteger(如下)的最佳途径,但这不起作用。

Would appreciate any help with a good way to do this. 感谢您提供帮助以好的方法。

    ByteBuffer bb = ByteBuffer.wrap(new byte[8]).order(ByteOrder.nativeOrder());
    bb.putLong(12345678910L);

    byte[] bytes = new byte[8];
    for(int i=0; i<8; i++){
        bytes[i] = bb.get(i);
    }

    BigInteger bi = new BigInteger(bytes);// bi is not correct

Try taking out the order call: 尝试取出订购电话:

    ByteBuffer bb = ByteBuffer.wrap(new byte[8]);
    bb.putLong(12345678910L);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM