https://kotlinlang.org logo
Title
d

dave08

01/01/2018, 2:00 PM
Out of the results of:
"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

elizarov

01/01/2018, 2:37 PM
"<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

dave08

01/01/2018, 2:39 PM
Perfect 👍🏼, thanks!
Actually this gives the same:
"<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

karelpeeters

01/02/2018, 7:26 AM
If you need leading zeroes in the hexadecimal numbers make sure to use
"%02x".format(it)
.
d

dave08

01/02/2018, 7:35 AM
@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

karelpeeters

01/02/2018, 7:38 AM
There is
toInt(16)
, bith without leading zeroes. Good luck!