I'm looking for a similar function to Java's Integer.toHexString()
in Kotlin. Is there something built-in, or we have to manually write a function to convert Int
to String
?
-
Note: I'm aware that I can use the `Integer` class from Java :) I'm looking for a pure Kotlin approach, if available. – milosmns Jan 14 '17 at 22:36
-
Most of the kotlin library simply consists in providing extension methods on top of standard Java classes. The language and library are designed to make it easy to use the Java classes. Why won't you use Integer.toHexString? What would an alternate implementation bring, except redundancy? – JB Nizet Jan 14 '17 at 22:52
-
@JBNizet I'm not against using `Integer.toHexString()` - For example, take a look at the answer that @hotkey provided below. Short and directly accessible from any Int. I can use anything really to complete my task, but I was just looking for something Kotlin-like if available. – milosmns Jan 14 '17 at 23:00
-
@milosmns, updated the answer. The `toString(radix: Int)` function will still be different because of the unsigned conversion used in `Integer.toHexString()`, so that it won't probably fit you anyway. – hotkey Jan 14 '17 at 23:02
-
Right, got it. That pretty much answers everything I wanted to know – milosmns Jan 14 '17 at 23:02
-
So, you want to define an extension method that calls Integer.toHexString()? Not reimplmeent the function in Kotlin? Here it is: `fun Int.toHexString() = Integer.toHexString(this)` – JB Nizet Jan 14 '17 at 23:02
-
@JBNizet Yep, that's exactly what I had so far. Thanks for the help guys – milosmns Jan 14 '17 at 23:03
4 Answers
You can still use the Java conversion by calling the static function on java.lang.Integer
:
val hexString = java.lang.Integer.toHexString(i)
And, starting with Kotlin 1.1, there is a function in the Kotlin standard library that does the conversion, too:
fun Int.toString(radix: Int): String
Returns a string representation of this
Int
value in the specifiedradix
.
Note, however, that this will still be different from Integer.toHexString()
, because the latter performs the unsigned conversion:
println((-50).toString(16)) // -32
println(Integer.toHexString(-50)) // ffffffce
But with experimental Kotlin unsigned types, it is now possible to get the same result from negative number unsigned conversion as with Integer.toHexString(-50)
:
println((-50).toUInt().toString(16)) // ffffffce
-
3You can get the same results as `Integer.toHexString()` by converting to long first: `(-50).toLong().toString(16)` returns `ffffffce`. Also, there is `UInt` and `ULong` now, so for a long, you could use `toULong().toString(16)`. – Tenfour04 Nov 26 '19 at 15:16
You can simply do it like this:
"%x".format(1234)

- 3,072
- 1
- 24
- 37
-
11And you can also use `"%X".format(1234)` if you want the hex string outputted in all caps. – Dhiraj Gupta Jan 10 '20 at 21:21
If you need to add zero before bytes which less than 10(hex), for example you need string - "0E" then use:
"%02x".format(14)

- 2,267
- 1
- 14
- 19
Here is the new Kotlin 1.9.0 standard library HexFormat
:
(don't forget to add @OptIn(ExperimentalStdlibApi::class)
where needed)
val myHexFormat = HexFormat {
upperCase = false
number.prefix = "#"
number.removeLeadingZeros = true
}
val myInt = 0xb40e89
myInt.toHexString(myHexFormat) // #b40e89
You could also use the Kotlin predefined HexFormat
s:
val myInt = 0xb40e89
myInt.toHexString(HexFormat.Default) // 00b40e89
myInt.toHexString(HexFormat.UpperCase) // 00B40E89
To exclude the alpha from an ARGB integer, do this workaround:
(myInt and 0xFFFFFF).toHexString(myHexFormat)
And vote for this issue: Cannot ignore alpha when formatting with HexFormat

- 18,032
- 13
- 118
- 133