How to convert hex string to long in Scala?

Scala | Convert Hex String to Long: Here, we are going to learn how to convert hex string to long in Scala?
Submitted by Shivang Yadav, on May 28, 2020 [Last updated : March 10, 2023]

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.

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.

Scala String Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.