Welcome to the forum :)
Yes, you can easily convert an int to a string by using $ - in Nim it's a convention to have a $ operator for a type when you want to define a proc to convert it to a string, so:
let myint = 5
let myintstr = $myint
echo myintstr
To go the other way (string to int) is a proc called parseInt(). To make it a bit more robust:
#
# str2int([string], [default result])
#
# . [default result] is an optional return value if parseInt() has an exception
# If not default result and exception then return 0
#
# Ex.
# str2int("1984") == > 1984
# str2int("", 5) == > 5
# str2int("") == > 0
#
proc str2int*(s: string, d: varargs[int]): int =
result = try: parseInt(s) except ValueError:
if len(d) > 0: d[0] else: 0
Your example should use a default argument, not varargs.
proc str2int*(s: string, d: int = 0): int =
result = try: parseInt(s) except ValueError: d
To go the other way (string to int) is a proc called parseInt(). To make it a bit more robust:
using varargs here works, but isn't optimal. A default param value is not only more clear regarding the intention, but it also doesn't allow for passing more than one extra parameter in.
proc str2int*(s: string, d = 0): int =
result = try: parseInt(s) except ValueError: d