ceran ceran - 19 days ago 4x
Scala Question

Extract Longs from ByteBuffer (Java/Scala)

I'm constructing

numbers that consist of two
s each in the following way:

val msb = -1L // some arbitrary long value, can be anything between Long.Min/MaxValue
val lsb = 25L // a second arbitrary long value

val bb = ByteBuffer
.put(0.toByte) // 1 byte
.putLong(msb) // 8 bytes
.putLong(lsb) // 8 bytes

val number = BigInt(bb.array) // in this case: 340282366920938463444927863358058659865

The reason I'm adding another 0-
at the front is to guarantee that the result is a positive number. Otherwise, the resulting
could be negative due to two's complement. An algorithm that is called afterwards expects numbers greater or equal than zero.

So far, so good.

I'm having trouble with reversing this whole process - transforming the
back to two
s (exactly the two values that were used as the input). I can't just do the following:

val arr = number.toByteArray
val bb = ByteBuffer.wrap(arr)
val ignore = bb.getByte
val msb = bb.getLong
val lsb = bb.getLong

Imagine the
number is e.g. 3. Then
would result in an Array of size 1, not 16 (or 17), and therefore the calls to
would cause a

What's the easiest way to solve this problem? I tried several ways to manually fill up the buffer until there are 16 bytes available, but since this "padding" must correctly take the two's complement of the two numbers into account, I wasn't succesful.


Modulo operation can be helpful here:

val number = BigInt(bb.array) // in this case: 340282366920938463444927863358058659865

val modulo = BigInt(2).pow(64)
val lsb2 = (number / modulo).toLong     //25
val msb2 = (number.mod(modulo)).toLong  //-1