Question

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.

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

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?

Was it helpful?

Solution

You can do like this

    ByteArrayInputStream bais = new ByteArrayInputStream(longBytes);
    DataInputStream dis = new DataInputStream(bais);
    someLong = dis.readLong();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top