mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-07 13:33:22 +00:00
fix toHex - make it work with int literals (#15770)
(cherry picked from commit c0fdc8b215)
This commit is contained in:
@@ -981,6 +981,18 @@ proc toHex*(x: BiggestInt, len: Positive): string {.noSideEffect,
|
||||
doAssert c.toHex(6) == "FFFFF8"
|
||||
toHexImpl(cast[BiggestUInt](x), len, x < 0)
|
||||
|
||||
proc toHex*(x: int, len: Positive): string {.noSideEffect.} =
|
||||
## Converts `x` to its hexadecimal representation.
|
||||
##
|
||||
## The resulting string will be exactly `len` characters long. No prefix like
|
||||
## ``0x`` is generated. `x` is treated as an unsigned value.
|
||||
runnableExamples:
|
||||
doAssert toHex(62, 3) == "03E"
|
||||
doAssert toHex(4097, 3) == "001"
|
||||
doAssert toHex(4097, 4) == "1001"
|
||||
doAssert toHex(-8, 6) == "FFFFF8"
|
||||
toHexImpl(cast[BiggestUInt](x), len, x < 0)
|
||||
|
||||
proc toHex*[T: SomeInteger](x: T): string {.noSideEffect.} =
|
||||
## Shortcut for ``toHex(x, T.sizeof * 2)``
|
||||
runnableExamples:
|
||||
|
||||
Reference in New Issue
Block a user