Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 747
em70 Avatar asked Mar 21 '12 17:03

em70


1 Answers

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.

like image 71
Travis Brown Avatar answered Sep 20 '22 17:09

Travis Brown