Capy-Bearer Documentation

u8

lib call

Capy

function u8(value : bool, opt : any = {}) u8
function u8(value : any, opt : any = {}) u8
function u8(value : as string, opt : any = {}) u8

u8() parses text in the range 0 through 255. Invalid, negative, or out-of-range text returns opt.fallback. The default fallback is 0.

An any value that holds a number converts without text when the base is 10. The result is the same as the result for the text of that number. true converts to 1 and false to 0, also inside an any.

A one-argument numeric conversion uses the number directly. A float truncates toward zero. An out-of-range or nonfinite input produces 0. An out-of-range integer literal is a compile error, so u8(256) does not compile. A numeric variable containing 256 converts to zero.

Text parsing trims surrounding whitespace and accepts a leading +. In base 10, decimal fractions and exponents are valid and truncate toward zero. Other bases require integer digits without prefixes such as 0x. The base must be a numeric whole value from 2 through 36.

For example, u8("256", fallback: 7) returns 7. Use a fallback when zero must not hide invalid input.

print(u8("ff", base: 16), "\n")

Output

255

Parameters

value : scalar value or text to convert

opt : options map with fallback and base

Return Values

An unsigned 8-bit integer, or opt.fallback.