如何在不使用java.nio的情况下将字节数组转换为long?

时间:2022-09-15 18:30:29

I am implementing a reliable data transfer protocol. I need to pass the checksum which is long value to a receiver. I am not allowed to use java.nio.

我正在实施可靠的数据传输协议。我需要将长值的校验和传递给接收器。我不允许使用java.nio。

I know how to convert long to byte array as show below:

我知道如何将long转换为byte数组,如下所示:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(someLong);
dos.close();
byte[] longBytes = baos.toByteArray();

But how do I convert byte array to long without using java.nio?

但是如何在不使用java.nio的情况下将字节数组转换为long?

1 个解决方案

#1


2  

You can do like this

你可以这样做

    ByteArrayInputStream bais = new ByteArrayInputStream(longBytes);
    DataInputStream dis = new DataInputStream(bais);
    someLong = dis.readLong();

#1


2  

You can do like this

你可以这样做

    ByteArrayInputStream bais = new ByteArrayInputStream(longBytes);
    DataInputStream dis = new DataInputStream(bais);
    someLong = dis.readLong();