String.toUint
func String.toUint(s: string, base: int = 10): uint64 | nullString.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.toIntreads, but takes no sign, not even+. Anything else,""included, givesnull. - Covers the whole
uint64range; a larger number isnull. - A
baseoutside2 ..= 36panics.
Basic examples
Parse a decimal number:
String.toUint("42") // 42The largest uint64 parses, though it does not fit an int:
String.toUint("18446744073709551615") // 18446744073709551615A sign is not a digit, so a negative number is null:
String.toUint("-1") // nullScope
Pass a base from 2 to 36. Above ten, the letters a to z are digits, in either case:
"101".toUint(2) // 5
"755".toUint(8) // 493
"ff".toUint(16) // 255
"FF".toUint(16) // 255
"ffffffffffffffff".toUint(16) // 18446744073709551615
"zz".toUint(36) // 1295A digit that is not one of the base is null, and so is a number one past Math.maxUint64:
"12".toUint(2) // null
"g".toUint(16) // null
"18446744073709551616".toUint() // nullLeading zeros are digits like any other:
"007".toUint() // 7
"0".toUint() // 0No sign, no prefix, no space, no _ separator, and no empty string:
"+1".toUint() // null
"-0".toUint() // null
"0x10".toUint() // null
" 1".toUint() // null
"1_000".toUint() // null
"".toUint() // nullOnly ASCII digits count. A digit of another script, or a full-width one, is null:
"٣".toUint() // null
"1".toUint() // nullCall it through ., in a pipe, with named arguments, or pass it as a function value:
"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:
"100".toUint() // 100
"100".toUint(base: 2) // 4
"100".toUint(base: 16) // 256Applications
Read a hex color into its three channels. ?? return null ends the function when the text is not hex:
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"))[255, 128, 0]
nullParse a 64-bit identifier written in hex, such as a trace ID, whose high bit is set and so does not fit an int:
let id = "9f86d081884c7d65".toUint(16) ?? panic("not hex")
println(id)
println(String.fromInt(id, 16))11495104353665842533
9f86d081884c7d65Read a port number, checking its range before converting to uint16:
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"))8080
null
nullRead a Unix permission mode in octal, the way chmod takes it:
for mode in ["755", "640", "abc"] {
let bits = mode.toUint(8)
if bits == null {
println("${mode}: not octal")
} else {
println("${mode}: ${bits}")
}
}755: 493
640: 416
abc: not octalProperties and relations
String.fromInt writes what String.toUint reads, in every base:
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:
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:
String.toInt("9223372036854775808") // null
String.toUint("9223372036854775808") // 9223372036854775808Possible issues
The result may be null, so it is not a uint64 until you narrow it:
let n: uint64 = "5".toUint() // error: cannot use uint64 | null where uint64 is expectedAn 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:
let n = "5".toUint() ?? 0
let m = n + 1 // error: + needs both operands of one type, got uint64 | int and intlet n = "5".toUint() ?? uint64(0)
println(n + uint64(1))6Converting the result to a narrower kind panics when it does not fit. Check the range first, as port does above:
let n = "300".toUint() ?? panic("not a number")
println(byte(n)) // panic: 300 out of range for byteA base outside 2 ..= 36 panics, even for text that would parse:
"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 37See also
String.toInt: parse a signedint.String.toFloat: parse a decimal number.String.fromInt: write an integer in a base.String.isDigit: test for decimal digits of any script.Math.toInt: afloat64as anint, ornull.
