diff --git a/src/dorkbox/bytes/HexExtensions.kt b/src/dorkbox/bytes/HexExtensions.kt index 0b48ccb..955c1d8 100644 --- a/src/dorkbox/bytes/HexExtensions.kt +++ b/src/dorkbox/bytes/HexExtensions.kt @@ -40,7 +40,7 @@ package dorkbox.bytes @JvmInline -public value class HexString(val string: String) +value class HexString(val string: String) object Hex { /** @@ -238,10 +238,68 @@ fun String.isValidHex(): Boolean = Hex.HEX_REGEX.matches(this) /** * Returns a HexString if a given string is a valid hex-string - either with or without 0x prefix */ -fun String.toHex(): HexString { +fun String.asHex(): HexString { if (!this.isValidHex()) { throw IllegalArgumentException("String is not hex") } return HexString(this) } + + +fun Byte.toHexString(prefix: String = "0x", toUpperCase: Boolean = false): String { + return if (toUpperCase) { + prefix + Hex.encodeUpper(this) + } else { + prefix + Hex.encode(this) + } +} +fun UByte.toHexString(prefix: String = "0x", toUpperCase: Boolean = false): String { + return if (toUpperCase) { + prefix + this.toString(16).uppercase() + } else { + prefix + this.toString(16) + } +} +fun Short.toHexString(prefix: String = "0x", toUpperCase: Boolean = false): String { + return if (toUpperCase) { + prefix + this.toString(16).uppercase() + } else { + prefix + this.toString(16) + } +} +fun UShort.toHexString(prefix: String = "0x", toUpperCase: Boolean = false): String { + return if (toUpperCase) { + prefix + this.toString(16).uppercase() + } else { + prefix + this.toString(16) + } +} +fun Int.toHexString(prefix: String = "0x", toUpperCase: Boolean = false): String { + return if (toUpperCase) { + prefix + this.toString(16).uppercase() + } else { + prefix + this.toString(16) + } +} +fun UInt.toHexString(prefix: String = "0x", toUpperCase: Boolean = false): String { + return if (toUpperCase) { + prefix + this.toString(16).uppercase() + } else { + prefix + this.toString(16) + } +} +fun Long.toHexString(prefix: String = "0x", toUpperCase: Boolean = false): String { + return if (toUpperCase) { + prefix + this.toString(16).uppercase() + } else { + prefix + this.toString(16) + } +} +fun ULong.toHexString(prefix: String = "0x", toUpperCase: Boolean = false): String { + return if (toUpperCase) { + prefix + this.toString(16).uppercase() + } else { + prefix + this.toString(16) + } +} diff --git a/test/dorkbox/bytes/TestHex.kt b/test/dorkbox/bytes/TestHex.kt index e6e9c07..7febc40 100644 --- a/test/dorkbox/bytes/TestHex.kt +++ b/test/dorkbox/bytes/TestHex.kt @@ -40,6 +40,19 @@ class TestHex { assertTrue(Hex.decode("0xab").contentEquals(Hex.decode("ab"))) } + @Test + fun testPrimatives() { + assertEquals("0x0", 0.toHexString()) + assertEquals("0x1", 1.toHexString()) + assertEquals("0xa", 10.toHexString()) + assertEquals("0xf", 15.toHexString()) + assertEquals("0x10", 16.toHexString()) + assertEquals("0x11", 17.toHexString()) + assertEquals("0xff", 255.toHexString()) + assertEquals("0x100", 256.toHexString()) + assertEquals("0x4e9", 1257.toHexString()) + } + @Test fun sizesAreOk() { assertEquals(0, Hex.decode("0x").size)