Scala 库将数字(Int、Long、Double)转换为/从 Array[Byte]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9810010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:01:17  来源:igfitidea点击:

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

scalaserialization

提问by em70

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?

正如标题所说,是否有任何 Scala 库可以导出函数以将字节数组(最好是流畅地)转换为 Int、Long 或 Double?

I need something compatible with 2.9.1 and FOSS.

我需要与 2.9.1 和 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! :)

如果您恰好知道我需要什么以及在哪里可以找到它,那么一行 SBT 和一行示例就足够了!:)

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

如果没有我正在寻找的东西,那么 Java 中最接近的东西也将起作用......

回答by Travis Brown

You can use Java NIO's ByteBuffer:

您可以使用Java NIO 的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.

不需要额外的依赖。

回答by Themerius

You can also use BigIntfrom the scala standard library.

您也可以BigInt从 Scala 标准库中使用。

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

回答by Rex Kerr

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

Javanio.ByteBuffer是目前要走的路:

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 putarrays of bytes, etc.

您还可以put使用字节数组等。

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

请记住小/大端的事情。 bb.order(java.nio.ByteOrder.nativeOrder)可能是你想要的。

回答by prosseek

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

对于 Double <-> ByteArray,您可以使用 java.lang.Double.doubleToLongBits 和 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:

或者可以使用 ByteBuffer:

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

回答by Jkulkarni

The following worked for me using Scala:

以下使用 Scala 对我有用:

import org.apache.kudu.client.Bytes

Bytes.getFloat(valueToConvert)

回答by user2012643

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

您还可以使用: Bytes.toInt(byteArray) 像魅力一样工作!