Added support for decimal primitives -> hex
parent
eac8851c92
commit
3db390bef9
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue