Scala十六进制字符串到字节

时间:2017-11-02 08:22:38

标签: scala protocol-buffers

在Scala中有一种巧妙的方法可以将十六进制编码的String转换为protobuf ByteString(并再次返回)吗?

3 个答案:

答案 0 :(得分:6)

您可以使用(没有其他依赖项)DatatypeConverter作为:

import com.google.protobuf.ByteString
import javax.xml.bind.DatatypeConverter
val hexString: String = "87C2D268483583714CD5"
val byteString: ByteString = ByteString.copyFrom(
  DatatypeConverter.parseHexBinary(hexString)
)
val originalString = DatatypeConverter.printHexBinary(byteString.toByteArray)

答案 1 :(得分:3)

您可以使用java.math.BigInteger来解析String,获取Array[Byte],然后将其转换为ByteString。这将是第一步:

import java.math.BigInteger

val s = "f263575e7b00a977a8e9a37e08b9c215feb9bfb2f992b2b8f11e"

val bs = new BigInteger(s, 16).toByteArray

bs的内容现在是:

Array(0, -14, 99, 87, 94, 123, 0, -87, 119, -88, -23, -93, 126, 8, -71, -62, 21, -2, -71, -65, -78, -7, -110, -78, -72, -15, 30)

然后,您可以使用(例如)copyFrom方法(JavaDoc here)将其转换为ByteString

答案 2 :(得分:0)

因为问题的标题没有提到Protobuf,所以如果有人正在寻找不需要任何依赖项的解决方案,则可以将任意大小的数组的十六进制String转换为Seq [Byte] :(请不要忘记必要时添加输入验证)

val zeroChar: Byte = '0'.toByte
val aChar: Byte = 'a'.toByte

def toHex(bytes: Seq[Byte]): String = bytes.map(b => f"$b%02x").mkString

def toBytes(hex: String): Seq[Byte] = {

  val lowerHex = hex.toLowerCase

  val (result: Array[Byte], startOffset: Int) =
    if (lowerHex.length % 2 == 1) {

    // Odd
    val r = new Array[Byte]((lowerHex.length >> 1) + 1)
    r(0) = toNum(lowerHex(0))
    (r, 1)

  } else {

    // Even
    (new Array[Byte](lowerHex.length >> 1), 0)
  }

  var inputIndex = startOffset
  var outputIndex = startOffset
  while (outputIndex < result.length) {

    val byteValue = (toNum(lowerHex(inputIndex)) * 16) +
      toNum(lowerHex(inputIndex + 1))

    result(outputIndex) = byteValue.toByte

    inputIndex += 2
    outputIndex += 1
  }

  result
}

def toNum(lowerHexChar: Char): Byte =
  (if (lowerHexChar < 'a') lowerHexChar.toByte - zeroChar else 10 +
    lowerHexChar.toByte - aChar).toByte

https://scalafiddle.io/sf/PZPHBlT/2