Hi. I would like to convert an Int to an hex stri...
# javascript
g
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
println("FF".toInt(16))
Ah, sorry, now I see
Copy code
fun Int.toString(radix: Int) = asDynamic().toString(radix)
👍 1
Copy code
println((255).toString(16))
b
@gaetan @konsoletyper But it wont work for negative values,
(-12).toString(16)
will return
-c
not
FF...F4
k
You can easily work-around this
For example,
(-12 and 0xFF).toString(16)
. Note that Kotlin JVM has the same behaviour
b
@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
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