Scala library to convert numbers (Int, Long, Double) to/from Array[Byte]

You can use Java NIO's ByteBuffer:

import java.nio.ByteBuffer

ByteBuffer.wrap(Array[Byte](1, 2, 3, 4)).getInt
ByteBuffer.wrap(Array[Byte](1, 2, 3, 4, 5, 6, 7, 8)).getDouble
ByteBuffer.wrap(Array[Byte](1, 2, 3, 4, 5, 6, 7, 8)).getLong

No extra dependencies required.


You can also use BigInt from the scala standard library.

import scala.math.BigInt
val bytearray = BigInt(1337).toByteArray
val int = BigInt(bytearray)

Java's nio.ByteBuffer is the way to go for now:

val bb = java.nio.ByteBuffer.allocate(4)
val i = 5
bb.putInt(i)
bb.flip  // now can read instead of writing
val j = bb.getInt
bb.clear // ready to go again

You can also put arrays of bytes, etc.

Keep in mind the little/big-endian thing. bb.order(java.nio.ByteOrder.nativeOrder) is probably what you want.