bamdmux
06/20/2017, 5:57 AMprivate val CHARS = "0123456789ABCDEF".toList().toCharArray()
fun Byte.toHex() = toInt().let { "${CHARS[it.shr(4).and(0x0F)]}${CHARS[it.and(0x0F)]}" }
fun List<Byte>.toHexString(sep: String = "") = map(Byte::toHex).joinToString(separator = sep)
fun String.hexList(): List<Byte> = with(filter { it != ':' }.toUpperCase()) {
if (length % 2 != 0) throw NumberFormatException("Odd number of characters: $length")
if (!all { it in CHARS }) throw NumberFormatException("Invalid characters: ${this@hexList}")
ByteArray(length / 2).mapIndexed { i, _ ->
((CHARS.indexOf(this[i * 2]).shl(4)) + (CHARS.indexOf(this[i * 2 + 1]))).toByte()
}
}