Skip to content

Convert an Integer to a Hexadecimal String in Kotlin

Posted on:June 1, 2023

Let’s go over some ways to convert an Integer into a Hexadecimal String.

1. java.lang.Integer.toHexString(int i)

We can use the static method toHexString on the Java Integer class and pass it the value to convert:

Integer.toHexString(2766) // "ace"

Link to Documentation: java.lang.Integer.toHexString(int i)

2. kotlin.Int.toString(radix: Int)

We can call the toString method on a Kotlin Int instance with a radix of 16 (hexadecimal):

2766.toString(16) // "ace"

Link to Documentation: kotlin.Int.toString(radix: Int)