How to convert Int to Hex String in Kotlin?

Kotlin Mobile DevelopmentApps/Applications

Kotlin is statistically typed language and it is built on JVM. Kotlin is cent percent comparable with Java. Hence, some of the Java functions can be used in Kotlin as well.

In this article, we will take an example to demonstrate how we can use a Java class function convert an Int in Kotlin to its correponding Hex String.

Example – Converting Int to Hex String in Kotlin

In this example, we will use the Java class function toHexString().

import java.lang.*

fun main(args: Array<String>) {

   val hexString = java.lang.Integer.toHexString(-66)
   println("Hex String for negative Number: " +hexString)

   val positiveNumber = java.lang.Integer.toHexString(166)
   println("Hex String for positive Number: " +positiveNumber)

}

Output

On execution, it will produce the following output −

Hex String for negative Number: ffffffbe
Hex String for positive Number: a6
raja
Updated on 01-Mar-2022 12:03:40

Advertisements