From 76c3bcefd73e0d54e1a70ac11976dc7a404ef764 Mon Sep 17 00:00:00 2001 From: Robinson Date: Sun, 2 Jul 2023 20:44:43 +0200 Subject: [PATCH] Added upper-case to hex extensions --- src/dorkbox/bytes/HexExtensions.kt | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/dorkbox/bytes/HexExtensions.kt b/src/dorkbox/bytes/HexExtensions.kt index c4abfad..887610e 100644 --- a/src/dorkbox/bytes/HexExtensions.kt +++ b/src/dorkbox/bytes/HexExtensions.kt @@ -52,24 +52,36 @@ object Hex { * Represents all the chars used for nibble */ private const val CHARS = "0123456789abcdef" + private const val CHARS_UPPER = "0123456789ABCDEF" internal val HEX_REGEX = Regex("(0[xX])?[0-9a-fA-F]*") /** - * Encodes the given byte value as an hexadecimal character. + * Encodes the given byte value as a hexadecimal character. */ fun encode(value: Byte): String { return CHARS[value.toInt().shr(4) and 0x0f].toString() + CHARS[value.toInt().and(0x0f)].toString() } + /** + * Encodes the given byte value as a hexadecimal character. + */ + fun encodeUpper(value: Byte): String { + return CHARS_UPPER[value.toInt().shr(4) and 0x0f].toString() + CHARS_UPPER[value.toInt().and(0x0f)].toString() + } + /** * Encodes the given byte array value to its hexadecimal representations, and prepends the given prefix to it. * * Note that by default the 0x prefix is prepended to the result of the conversion. * If you want to have the representation without the 0x prefix, pass to this method an empty prefix. */ - fun encode(value: ByteArray, prefix: String = "0x"): String { - return prefix + value.joinToString("") { encode(it) } + fun encode(value: ByteArray, prefix: String = "0x", toUpperCase: Boolean = false): String { + return if (toUpperCase) { + prefix + value.joinToString("") { encodeUpper(it) } + } else { + prefix + value.joinToString("") { encode(it) } + } } /** @@ -115,12 +127,12 @@ object Hex { * If you want to have the representation without the 0x prefix, use the [toNoPrefixHexString] method or * pass to this method an empty [prefix]. */ -fun ByteArray.toHexString(prefix: String = "0x"): String = Hex.encode(this, prefix) +fun ByteArray.toHexString(prefix: String = "0x", toUpperCase: Boolean = false): String = Hex.encode(this, prefix, toUpperCase) /** * Converts [this] [ByteArray] into its hexadecimal representation without prepending any prefix to it. */ -fun ByteArray.toNoPrefixHexString(): String = toHexString(prefix = "") +fun ByteArray.toNoPrefixHexString(toUpperCase: Boolean = false): String = Hex.encode(this, "", toUpperCase) /** @@ -135,7 +147,7 @@ fun Collection.toHexString(prefix: String = "0x"): String = Hex.encode(thi /** * Converts [this] [Collection] of bytes into its hexadecimal representation without prepending any prefix to it. */ -fun Collection.toNoPrefixHexString(): String = toHexString("") +fun Collection.toNoPrefixHexString(): String = Hex.encode(this.toByteArray(), "") /**