summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorMiran <narimiran@disroot.org>2020-10-30 10:04:53 +0100
committerGitHub <noreply@github.com>2020-10-30 10:04:53 +0100
commitc0fdc8b215b02736cdb1c275a92b1d5a513f8bad (patch)
tree01b53b40b8e9472df8eb60c240e1d32f3628afa7 /lib
parent4374c638cd328b8cf9b72ff756cfe0564d6d9c78 (diff)
downloadNim-c0fdc8b215b02736cdb1c275a92b1d5a513f8bad.tar.gz
fix `toHex` - make it work with int literals (#15770)
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/strutils.nim12
1 files changed, 12 insertions, 0 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim
index 20ad2f517..94bdafc10 100644
--- a/lib/pure/strutils.nim
+++ b/lib/pure/strutils.nim
@@ -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: