Skip to content

String.toUint ​

ant
func String.toUint(s: string, base: int = 10): uint64 | null

String.toUint parses s as an unsigned integer in base, or returns null when it is not one. s is digits of base as for String.toInt, with no sign. A number that does not fit a uint64 is null too. base defaults to 10, and it panics if base is outside 2 ..= 36.

Details ​

  • Reads the digits that String.toInt reads, but takes no sign, not even +. Anything else, "" included, gives null.
  • Covers the whole uint64 range; a larger number is null.
  • A base outside 2 ..= 36 panics.

Basic examples ​

Parse a decimal number:

ant
String.toUint("42") // 42

The largest uint64 parses, though it does not fit an int:

ant
String.toUint("18446744073709551615") // 18446744073709551615

A sign is not a digit, so a negative number is null:

ant
String.toUint("-1") // null

Scope ​

Pass a base from 2 to 36. Above ten, the letters a to z are digits, in either case:

ant
"101".toUint(2)               // 5
"755".toUint(8)               // 493
"ff".toUint(16)               // 255
"FF".toUint(16)               // 255
"ffffffffffffffff".toUint(16) // 18446744073709551615
"zz".toUint(36)               // 1295

A digit that is not one of the base is null, and so is a number one past Math.maxUint64:

ant
"12".toUint(2)                  // null
"g".toUint(16)                  // null
"18446744073709551616".toUint() // null

Leading zeros are digits like any other:

ant
"007".toUint() // 7
"0".toUint()   // 0

No sign, no prefix, no space, no _ separator, and no empty string:

ant
"+1".toUint()    // null
"-0".toUint()    // null
"0x10".toUint()  // null
" 1".toUint()    // null
"1_000".toUint() // null
"".toUint()      // null

Only ASCII digits count. A digit of another script, or a full-width one, is null:

ant
"٣".toUint()  // null
"1".toUint() // null

Call it through ., in a pipe, with named arguments, or pass it as a function value:

ant
"ff".toUint(16)                     // 255
"42".toUint()                       // 42
"777" | String.toUint(base: 8)      // 511
String.toUint(s: "1010", base: 2)   // 10
["1", "x", "30"].map(String.toUint) // [1, null, 30]
["a", "b"].map({ x.toUint(16) })    // [10, 11]

Named arguments ​

base ​

base picks the radix, from 2 to 36, and defaults to 10. The same text reads differently in each:

ant
"100".toUint()         // 100
"100".toUint(base: 2)  // 4
"100".toUint(base: 16) // 256

Applications ​

Read a hex color into its three channels. ?? return null ends the function when the text is not hex:

ant
func rgb(hex: string): Array[byte] | null {
  if hex.length() != 6 {
    return null
  }
  let n = hex.toUint(16) ?? return null
  [
    byte(Bits.shiftRight(n, 16)),
    byte(Bits.and(Bits.shiftRight(n, 8), uint64(255))),
    byte(Bits.and(n, uint64(255))),
  ]
}

println(rgb("ff8000"))
println(rgb("00zz00"))
text
[255, 128, 0]
null

Parse a 64-bit identifier written in hex, such as a trace ID, whose high bit is set and so does not fit an int:

ant
let id = "9f86d081884c7d65".toUint(16) ?? panic("not hex")
println(id)
println(String.fromInt(id, 16))
text
11495104353665842533
9f86d081884c7d65

Read a port number, checking its range before converting to uint16:

ant
func port(s: string): uint16 | null {
  let n = s.toUint() ?? return null
  if n > uint64(65535) {
    return null
  }
  uint16(n)
}

println(port("8080"))
println(port("70000"))
println(port("-1"))
text
8080
null
null

Read a Unix permission mode in octal, the way chmod takes it:

ant
for mode in ["755", "640", "abc"] {
  let bits = mode.toUint(8)
  if bits == null {
    println("${mode}: not octal")
  } else {
    println("${mode}: ${bits}")
  }
}
text
755: 493
640: 416
abc: not octal

Properties and relations ​

String.fromInt writes what String.toUint reads, in every base:

ant
for n in [uint64(0), uint64(1), uint64(255), Math.maxUint64] {
  for base in [2, 8, 10, 16, 36] {
    assert(String.fromInt(n, base).toUint(base) == n)
  }
}

On text without a sign, String.toUint agrees with String.toInt wherever the number fits an int:

ant
for s in ["0", "42", "9223372036854775807"] {
  let u = s.toUint() ?? panic("unreachable")
  assert(s.toInt() == int(u))
}

Above Math.maxInt, only String.toUint gives a number:

ant
String.toInt("9223372036854775808")  // null
String.toUint("9223372036854775808") // 9223372036854775808

Possible issues ​

The result may be null, so it is not a uint64 until you narrow it:

ant
let n: uint64 = "5".toUint() // error: cannot use uint64 | null where uint64 is expected

An integer literal is always an int, so ?? 0 gives uint64 | int, and arithmetic on it is a compile error. Write the default as a uint64:

ant
let n = "5".toUint() ?? 0
let m = n + 1 // error: + needs both operands of one type, got uint64 | int and int
ant
let n = "5".toUint() ?? uint64(0)
println(n + uint64(1))
text
6

Converting the result to a narrower kind panics when it does not fit. Check the range first, as port does above:

ant
let n = "300".toUint() ?? panic("not a number")
println(byte(n)) // panic: 300 out of range for byte

A base outside 2 ..= 36 panics, even for text that would parse:

ant
"1".toUint(1)  // panic: String.toUint needs a base from 2 to 36, got 1
"1".toUint(37) // panic: String.toUint needs a base from 2 to 36, got 37

See also ​