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?

Était-ce utile?

La solution

You can do like this

    ByteArrayInputStream bais = new ByteArrayInputStream(longBytes);
    DataInputStream dis = new DataInputStream(bais);
    someLong = dis.readLong();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top