Q:

How to convert hex string to long in Scala?

belongs to collection: Scala String Programs

0

Hex String

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

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.

Convert hex string to long in Scala

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

  • Step1: We will convert the hex string to integer values using the parseInt() method.
  • Step2: Then, we will convert this integer value to long value using the toLong method.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to convert hex string to long in Scala

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Scala String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to convert Int to Double in Scala?... >>
<< How to convert hex string to int in Scala?...