Out of the results of: ``` "0101-1002-0909-...
# announcements
d
Out of the results of:
Copy code
"0101-1002-0909-0210-1115-1511".split("-")
            .flatMap { it.chunked(2).map { it.toInt() } }
        	.map { if (it > 9) (it + 55).toChar() else (it + 48).toChar() }
        	.chunked(2)
I need:
11:A2:99:2A:BF:FB
, how could I do that idiomatically, also, is there a simpler algo for this? It's converting to hex..
e
Copy code
"<tel:0101-1002-0909|0101-1002-0909>-0210-1115-1511".split("-")
        .map { it.chunked(2)
            .map { it.toInt().toString(16).toUpperCase() }
            .joinToString(separator = "")
        }.joinToString(separator = ":")
d
Perfect 👍🏼, thanks!
Actually this gives the same:
Copy code
"<tel:0101-1002-0909|0101-1002-0909>-0210-1115-1511".split("-")
				.joinToString(separator = ":") { it.chunked(2)
						.joinToString(separator = "") { it.toInt().toString(16).toUpperCase() }
				}
But your is maybe clearer.. is there a difference in performance? (I'm using this in a Vert.x api...)
k
If you need leading zeroes in the hexadecimal numbers make sure to use
"%02x".format(it)
.
d
@karelpeeters I don't need those in the to hex algo, but I will need a way to convert the hex back to the number format, there I will need those zeros... my original code for that is not ideomatic at all (no
toInt(16)
, or is there?), but your hint might help, thanks!
k
There is
toInt(16)
, bith without leading zeroes. Good luck!