refactored int codecs to DRY and easy support signed/unsigned values

This commit is contained in:
sergeych committed 2023-03-11 14:35:37 +01:00
1 parent 0f9702b1dc
commit 8fb052c4f9
9 files changed
+148 -108

No files matched your search

@@ -29,11 +29,12 @@ private val hexDigits = "0123456789ABCDEF"
fun Long.encodeToHex(length: Int = 0): String {
var result = ""
var value = this
if (value < 0) throw IllegalArgumentException("cant convert to hex negative (ambiguous)")
var end = if( value >= 0 ) 0L else -1L
// if (value < 0) throw IllegalArgumentException("cant convert to hex negative (ambiguous)")
do {
result = hexDigits[(value and 0x0f).toInt()] + result
value = value shr 4
} while (value > 0)
} while (value != end)
while (result.length < length) result = "0" + result
return result
}