Merge pull request #2164 from oderwat/fix-tohex
Fixing toHex() to not wrap for long lens + Test
This commit is contained in:
commit
43c023c8dd
2 changed files with 32 additions and 27 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".} =
|
||||
|
|
|
|||
|
|
@ -43,6 +43,9 @@ assert "/1/2/3".rfind('/') == 4
|
|||
assert "/1/2/3".rfind('/', 1) == 0
|
||||
assert "/1/2/3".rfind('0') == -1
|
||||
|
||||
assert(toHex(100i16, 32) == "00000000000000000000000000000064")
|
||||
assert(toHex(-100i16, 32) == "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9C")
|
||||
|
||||
main()
|
||||
#OUT ha/home/a1xyz/usr/bin
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue