Added support for byte-array hex conversion taking an upper limit

This commit is contained in:
Robinson 2023-07-13 01:39:54 +02:00
parent 165b8ce2c9
commit 81d7063005
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C
3 changed files with 49 additions and 34 deletions

16
LICENSE
View File

@ -26,14 +26,6 @@
Andreas Schildbach Andreas Schildbach
ligi ligi
- Kotlin -
[The Apache Software License, Version 2.0]
https://github.com/JetBrains/kotlin
Copyright 2020
JetBrains s.r.o. and Kotlin Programming Language contributors
Kotlin Compiler, Test Data+Libraries, and Tools repository contain third-party code, to which different licenses may apply
See: https://github.com/JetBrains/kotlin/blob/master/license/README.md
- Netty - An event-driven asynchronous network application framework - Netty - An event-driven asynchronous network application framework
[The Apache Software License, Version 2.0] [The Apache Software License, Version 2.0]
https://netty.io https://netty.io
@ -69,6 +61,14 @@
Copyright 2023 Copyright 2023
Lightweight Java Game Library Lightweight Java Game Library
- Kotlin -
[The Apache Software License, Version 2.0]
https://github.com/JetBrains/kotlin
Copyright 2020
JetBrains s.r.o. and Kotlin Programming Language contributors
Kotlin Compiler, Test Data+Libraries, and Tools repository contain third-party code, to which different licenses may apply
See: https://github.com/JetBrains/kotlin/blob/master/license/README.md
- Updates - Software Update Management - Updates - Software Update Management
[The Apache Software License, Version 2.0] [The Apache Software License, Version 2.0]
https://git.dorkbox.com/dorkbox/Updates https://git.dorkbox.com/dorkbox/Updates

View File

@ -76,11 +76,11 @@ object Hex {
* Note that by default the 0x prefix is prepended to the result of the conversion. * 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. * 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", toUpperCase: Boolean = false): String { fun encode(value: ByteArray, prefix: String = "0x", limit: Int = value.size, toUpperCase: Boolean = false): String {
return if (toUpperCase) { return if (toUpperCase) {
prefix + value.joinToString("") { encodeUpper(it) } prefix + value.joinToString(separator = "", limit = limit, truncated = "") { encodeUpper(it) }
} else { } else {
prefix + value.joinToString("") { encode(it) } prefix + value.joinToString(separator = "", limit = limit, truncated = "") { encode(it) }
} }
} }
@ -127,12 +127,12 @@ object Hex {
* If you want to have the representation without the 0x prefix, use the [toNoPrefixHexString] method or * If you want to have the representation without the 0x prefix, use the [toNoPrefixHexString] method or
* pass to this method an empty [prefix]. * pass to this method an empty [prefix].
*/ */
fun ByteArray.toHexString(prefix: String = "0x", toUpperCase: Boolean = false): String = Hex.encode(this, prefix, toUpperCase) fun ByteArray.toHexString(prefix: String = "0x", limit: Int = this.size, toUpperCase: Boolean = false): String = Hex.encode(this, prefix, limit, toUpperCase)
/** /**
* Converts [this] [ByteArray] into its hexadecimal representation without prepending any prefix to it. * Converts [this] [ByteArray] into its hexadecimal representation without prepending any prefix to it.
*/ */
fun ByteArray.toNoPrefixHexString(toUpperCase: Boolean = false): String = Hex.encode(this, "", toUpperCase) fun ByteArray.toNoPrefixHexString(limit: Int = this.size, toUpperCase: Boolean = false): String = Hex.encode(this, "", limit, toUpperCase)
/** /**
@ -142,12 +142,12 @@ fun ByteArray.toNoPrefixHexString(toUpperCase: Boolean = false): String = Hex.en
* If you want to have the representation without the 0x prefix, use the [toNoPrefixHexString] method or * If you want to have the representation without the 0x prefix, use the [toNoPrefixHexString] method or
* pass to this method an empty [prefix]. * pass to this method an empty [prefix].
*/ */
fun Collection<Byte>.toHexString(prefix: String = "0x"): String = Hex.encode(this.toByteArray(), prefix) fun Collection<Byte>.toHexString(prefix: String = "0x", limit: Int = this.size): String = Hex.encode(this.toByteArray(), prefix, limit)
/** /**
* Converts [this] [Collection] of bytes into its hexadecimal representation without prepending any prefix to it. * Converts [this] [Collection] of bytes into its hexadecimal representation without prepending any prefix to it.
*/ */
fun Collection<Byte>.toNoPrefixHexString(): String = Hex.encode(this.toByteArray(), "") fun Collection<Byte>.toNoPrefixHexString(limit: Int = this.size): String = Hex.encode(this.toByteArray(), "", limit)
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2021 dorkbox, llc * Copyright 2023 dorkbox, llc
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,16 +23,16 @@ class TestHex {
@Test @Test
fun weCanProduceSingleDigitHex() { fun weCanProduceSingleDigitHex() {
assertEquals(Hex.encode(0.toByte()), "00") assertEquals("00", Hex.encode(0.toByte()))
assertEquals(Hex.encode(1.toByte()), "01") assertEquals("01", Hex.encode(1.toByte()))
assertEquals(Hex.encode(15.toByte()), "0f") assertEquals("0f", Hex.encode(15.toByte()))
} }
@Test @Test
fun weCanProduceDoubleDigitHex() { fun weCanProduceDoubleDigitHex() {
assertEquals(Hex.encode(16.toByte()), "10") assertEquals("10", Hex.encode(16.toByte()))
assertEquals(Hex.encode(42.toByte()), "2a") assertEquals("2a", Hex.encode(42.toByte()))
assertEquals(Hex.encode(255.toByte()), "ff") assertEquals("ff", Hex.encode(255.toByte()))
} }
@Test @Test
@ -42,14 +42,29 @@ class TestHex {
@Test @Test
fun sizesAreOk() { fun sizesAreOk() {
assertEquals(Hex.decode("0x").size, 0) assertEquals(0, Hex.decode("0x").size)
assertEquals(Hex.decode("ff").size, 1) assertEquals(1, Hex.decode("ff").size)
assertEquals(Hex.decode("ffaa").size, 2) assertEquals(2, Hex.decode("ffaa").size)
assertEquals(Hex.decode("ffaabb").size, 3) assertEquals(3, Hex.decode("ffaabb").size)
assertEquals(Hex.decode("ffaabb44").size, 4) assertEquals(4, Hex.decode("ffaabb44").size)
assertEquals(Hex.decode("0xffaabb4455").size, 5) assertEquals(5, Hex.decode("0xffaabb4455").size)
assertEquals(Hex.decode("0xffaabb445566").size, 6) assertEquals(6, Hex.decode("0xffaabb445566").size)
assertEquals(Hex.decode("ffaabb44556677").size, 7) assertEquals(7, Hex.decode("ffaabb44556677").size)
}
@Test
fun byteArrayLimitWorks() {
assertEquals("0x", Hex.encode(Hex.decode("00"), limit = 0))
assertEquals("0x00", Hex.encode(Hex.decode("00"), limit = 1))
assertEquals("0x", Hex.encode(Hex.decode("ff"), limit = 0))
assertEquals("0xff", Hex.encode(Hex.decode("ff"), limit = 1))
assertEquals("0x", Hex.encode(Hex.decode("abcdef"), limit = 0))
assertEquals("0xab", Hex.encode(Hex.decode("abcdef"), limit = 1))
assertEquals("0xabcd", Hex.encode(Hex.decode("abcdef"), limit = 2))
assertEquals("0xabcdef", Hex.encode(Hex.decode("abcdef"), limit = 3))
assertEquals("0xabcdef", Hex.encode(Hex.decode("abcdef"), limit = 32))
assertEquals("0xaa12456789bb", Hex.encode(Hex.decode("0xaa12456789bb"), limit = 6))
assertEquals("0xaa12456789bb", Hex.encode(Hex.decode("0xaa12456789bb"), limit = 9))
} }
@Test @Test
@ -65,10 +80,10 @@ class TestHex {
@Test @Test
fun testRoundTrip() { fun testRoundTrip() {
assertEquals(Hex.encode(Hex.decode("00")), "0x00") assertEquals("0x00", Hex.encode(Hex.decode("00")))
assertEquals(Hex.encode(Hex.decode("ff")), "0xff") assertEquals("0xff", Hex.encode(Hex.decode("ff")))
assertEquals(Hex.encode(Hex.decode("abcdef")), "0xabcdef") assertEquals("0xabcdef", Hex.encode(Hex.decode("abcdef")))
assertEquals(Hex.encode(Hex.decode("0xaa12456789bb")), "0xaa12456789bb") assertEquals("0xaa12456789bb", Hex.encode(Hex.decode("0xaa12456789bb")))
} }
@Test @Test