em70
em70

Reputation: 6081

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

As the title says, is there any Scala library that exports functions to convert, preferably fluently, a byte array to an Int, to a Long or to a Double?

I need something compatible with 2.9.1 and FOSS.

If you happen to know exactly what I need and where to find it, a line for SBT and a line for an example will be enough! :)

If there's no such thing as what I'm looking for, the closest thing in Java will also work...

Upvotes: 41

Views: 39122

Answers (6)

Jkulkarni
Jkulkarni

Reputation: 1

The following worked for me using Scala:

import org.apache.kudu.client.Bytes

Bytes.getFloat(valueToConvert)

Upvotes: 0

user2012643
user2012643

Reputation: 39

You can also use: Bytes.toInt(byteArray) Worked like a charm!

Upvotes: -1

prosseek
prosseek

Reputation: 190659

For Double <-> ByteArray, you can use java.lang.Double.doubleToLongBits and java.lang.Double.longBitsToDouble.

import java.lang.Double

def doubleToByteArray(x: Double) = {
    val l = java.lang.Double.doubleToLongBits(x)
    val a = Array.fill(8)(0.toByte)
    for (i <- 0 to 7) a(i) = ((l >> ((7 - i) * 8)) & 0xff).toByte
    a
}

def byteArrayToDouble(x: Array[scala.Byte]) = {
    var i = 0
    var res = 0.toLong
    for (i <- 0 to 7) {
        res +=  ((x(i) & 0xff).toLong << ((7 - i) * 8))
    }
    java.lang.Double.longBitsToDouble(res)
}

scala> val x = doubleToByteArray(12.34)
x: Array[Byte] = Array(64, 40, -82, 20, 122, -31, 71, -82)

scala> val y = byteArrayToDouble(x)
y: Double = 12.34

Or ByteBuffer can be used:

import java.nio.ByteBuffer
def doubleToByteArray(x: Double) = {
  val l = java.lang.Double.doubleToLongBits(x)
  ByteBuffer.allocate(8).putLong(l).array()
}

def byteArrayToDouble(x:Array[Byte]) = ByteBuffer.wrap(x).getDouble

Upvotes: 6

Themerius
Themerius

Reputation: 1911

You can also use BigInt from the scala standard library.

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

Upvotes: 40

Rex Kerr
Rex Kerr

Reputation: 167871

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.

Upvotes: 22

Travis Brown
Travis Brown

Reputation: 139028

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.

Upvotes: 57

Related Questions