Fixing toHex() to not wrap for long lens.
If you specify a len like 32 toHex() will repeat the given value in the output. Besides that I believe my implementation is easier and seems not to change how negative numbers are handled. I also handle the case of wrapping negative number beyond BiggestInt to "do it right".
This commit is contained in:
parent
b7f11b8b0a
commit
88f3b1d99f
1 changed files with 5 additions and 3 deletions
|
|
@ -395,11 +395,13 @@ proc toHex*(x: BiggestInt, len: int): string {.noSideEffect,
|
|||
const
|
||||
HexChars = "0123456789ABCDEF"
|
||||
var
|
||||
shift: BiggestInt
|
||||
n = x
|
||||
result = newString(len)
|
||||
for j in countdown(len-1, 0):
|
||||
result[j] = HexChars[toU32(x shr shift) and 0xF'i32]
|
||||
shift = shift + 4
|
||||
result[j] = HexChars[n and 0xF]
|
||||
n = n shr 4
|
||||
# handle negative overflow
|
||||
if n == 0 and x < 0: n = -1
|
||||
|
||||
proc intToStr*(x: int, minchars: int = 1): string {.noSideEffect,
|
||||
rtl, extern: "nsuIntToStr".} =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue