I'm trying to convert the Long to array byte. This code block is working but this solution is a Java solution. I'm looking for a good solution in Scala. How can I convert the Long to array byte in Scala way?
val arrayByteFromLong: Array[Byte] = ByteBuffer.allocate(8).putLong(myLong).array()
You can leverage scala.math.BigInt:
import scala.math.BigInt
val arrayByteFromLong: Array[Byte] = BigInt(myLong).toByteArray
If you want to also pad the array to 8 Bytes you can do (quick-and-dirty not so efficient version):
arrayByteFromLong.reverse.padTo(8,0).reverse
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With