scala字符串转long_如何在Scala中将十六进制字符串转换为long?

scala字符串转long

十六进制字符串 (Hex String)

Hex String also is known as the hexadecimal string is a string of hexadecimal digits i.e. base 16 numbers.

十六进制字符串也被称为十六进制字符串,是十六进制数字的字符串,即以16为基数的数字。

Example:

例:

    string = "49AD1"

长整数 (Long Integers)

Long integers are storages for large integer values. It can store 64-bit signed integer value. The maximum value it can store is 9223372036854775807.

长整数是大整数值的存储。 它可以存储64位有符号整数值。 它可以存储的最大值是9223372036854775807。

在Scala中将十六进制字符串转换为long (Convert hex string to long in Scala)

We can convert a hex string to long in Scala by following these steps,

通过执行以下步骤,我们可以在Scala中将十六进制字符串转换为long,

  • Step1: We will convert the hex string to integer values using the parseInt() method.

    步骤1:我们将使用parseInt()方法将十六进制字符串转换为整数值。

  • Step2: Then, we will convert this integer value to long value using the toLong method.

    步骤2:然后,我们将使用toLong方法将此整数值转换为long值。

Program to convert hex string to long in Scala

在Scala中将十六进制字符串转换为long的程序

object MyObject {
    def main(args: Array[String]) {
        val hexString : String = "42e576f7"
        println("The Hex String is " + hexString)
        val intVal : Int = Integer.parseInt(hexString, 16)
        val longInt  = intVal.toLong
        println("HexString to Long value : " + longInt)
    }
}

Output:

输出:

The Hex String is 42e576f7
HexString to Long value : 1122334455

Explanation:

说明:

In the above code, we have created a hexadecimal string named hexString. Then we have converted hexString to integer value using the parseInt() method of the Integer class and store it to intVal. This intVal is converted to a Long Integer value using the toLong method and prints the value using println method.

在上面的代码中,我们创建了一个名为hexString的十六进制字符串。 然后,我们使用Integer类的parseInt()方法将hexString转换为整数值,并将其存储到intVal 。 使用toLong方法将此intVal转换为Long Integer值,并使用println方法将其打印出来。

翻译自: https://www.includehelp.com/scala/convert-hex-string-to-long.aspx

scala字符串转long