Title
g

gaetan

04/17/2017, 8:54 AM
Hi. I would like to convert an Int to an hex string. I found the
String.toInt(radix:Int)
but didn’t find the opposite.
k

konsoletyper

04/17/2017, 8:57 AM
println("FF".toInt(16))
Ah, sorry, now I see
fun Int.toString(radix: Int) = asDynamic().toString(radix)
👍 1
println((255).toString(16))
b

bamdmux

04/17/2017, 1:00 PM
@gaetan @konsoletyper But it wont work for negative values,
(-12).toString(16)
will return
-c
not
FF...F4
k

konsoletyper

04/17/2017, 1:01 PM
You can easily work-around this
For example,
(-12 and 0xFF).toString(16)
. Note that Kotlin JVM has the same behaviour
b

bamdmux

04/17/2017, 2:20 PM
@konsoletyper Yeah, I know thats a JVM problem, just was warning him
And that will print F4,
(-12).and(0xFFFFFFFF).toString(16)
will return
-c
@gaetan If you want to work with hexadecimal representations, your best option will be using List<Byte> and then converting byte to byte
byte.toInt().and(0xFF).toString(16)
g

gaetan

04/17/2017, 2:27 PM
Thanks for the proposal. In fact, it’s just to convert a color from an Int to its hex string representation.
The @konsoletyper solution do the job.
👍 1