All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sop.util.HexUtil.kt Maven / Gradle / Ivy

There is a newer version: 10.0.3
Show newest version
// SPDX-FileCopyrightText: 2023 Paul Schaub 
//
// SPDX-License-Identifier: Apache-2.0

package sop.util

class HexUtil {

    companion object {
        /**
         * Encode a byte array to a hex string.
         *
         * @param bytes bytes
         * @return hex encoding
         * @see
         *   [Convert Byte Arrays to Hex Strings in Kotlin](https://www.baeldung.com/kotlin/byte-arrays-to-hex-strings)
         */
        @JvmStatic fun bytesToHex(bytes: ByteArray): String = bytes.toHex()

        /**
         * Decode a hex string into a byte array.
         *
         * @param s hex string
         * @return decoded byte array
         * @see
         *   [Kotlin convert hex string to ByteArray](https://stackoverflow.com/a/66614516/11150851)
         */
        @JvmStatic fun hexToBytes(s: String): ByteArray = s.decodeHex()
    }
}

fun String.decodeHex(): ByteArray {
    check(length % 2 == 0) { "Hex encoding must have even number of digits." }

    return chunked(2).map { it.toInt(16).toByte() }.toByteArray()
}

fun ByteArray.toHex(): String = joinToString(separator = "") { eachByte -> "%02X".format(eachByte) }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy