Kotlin convert hex string to ByteArray

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()
}
  1. Split the string into 2-character pairs, representing each byte.
  2. Parse each hex pair to their integer values.
  3. Convert the parsed Ints to Bytes.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)