scala 字符串转换数组_如何在Scala中将十六进制字符串转换为字节数组?
scala 字符串轉換數組
Hex String in Scala denotes value in hexadecimal number system i.e. base 16 number system.
Scala中的十六進制字符串表示以十六進制數表示的值,即以16進制數表示的系統。
Example:
例:
hexString = "32AF1"Byte Array is an array that stores elements of byte data type.
字節數組是一個存儲字節數據類型元素的數組。
將十六進制字符串轉換為字節數組 (Converting Hex String to Byte Array)
We can convert a hex string to a byte array in Scala using some method from java libraries which is valid as Scala uses the java libraries for most of its functions.
我們可以使用Java庫中的某些方法在Scala中將十六進制字符串轉換為字節數組,這是有效的,因為Scala將Java庫用于其大多數功能。
Step 1: Convert hexadecimal string to int
步驟1: 將十六進制字符串轉換為int
Step 2: Convert integer value to byte array using the toByteArray method for BigInteger values.
步驟2:使用BigInteger值的toByteArray方法將整數值轉換為字節數組。
Program:
程序:
import scala.math.BigIntobject MyClass {def main(args: Array[String]) {val hexString = "080A4C";println("hexString : "+ hexString)val integerValue = Integer.parseInt(hexString, 16)val byteArray = BigInt(integerValue).toByteArrayprintln("The byte Array for the given hexString is : ")for(i <- 0 to byteArray.length-1 )print(byteArray(i)+ " ")} }Output:
輸出:
hexString : 080A4C The byte Array for the given hexString is : 8 10 76Description:
描述:
In the above code, we have a hexadecimal string named hexString, and then convert it to integer value using parseInt() method of Integer class and stored the value to a variable named integerValue. We will convert this integer value to byteArray using the toByteArray method of BigInt class and store it to a variable named byteArray and printed the value using print() method.
在上面的代碼中,我們有一個名為hexString的十六進制字符串,然后使用Integer類的parseInt()方法將其轉換為整數值,并將該值存儲到一個名為integerValue的變量中。 我們將使用BigInt類的toByteArray方法將此整數值轉換為byteArray,并將其存儲到名為byteArray的變量中,并使用print()方法打印該值。
翻譯自: https://www.includehelp.com/scala/convert-hex-string-to-byte-array.aspx
scala 字符串轉換數組
總結
以上是生活随笔為你收集整理的scala 字符串转换数组_如何在Scala中将十六进制字符串转换为字节数组?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: duration java_Java D
- 下一篇: Eratosthenes筛