You can handle it like this:
fun String.decodeHex(): ByteArray {
check(length % 2 == 0) { "Must have an even length" }
return chunked(2)
.map { it.toInt(16).toByte() }
.toByteArray()
}
- Split the string into 2-character pairs, representing each byte.
- Parse each hex pair to their integer values.
- Convert the parsed
Int
s toBytes
.