CBYTE
Converts a given value to a value that can fit within a byte
Implemented by: FreeBASIC, Gambas
With variations:
Also written as: __Cbyte
under FreeBASIC's -lang qb compiler option
Usage
CBYTE turns a value into a byte-sized value — whatever this might account for.
FreeBASIC wraps the given value into a signed byte value. Officially, _"results are undefined"_ for values lesser than -128 or greater than 127, and there is no checking for overflows, but the compiler issues warnings if the value is greater than the unsigned byte limit (255).
PRINT CBYTE(127) '127 PRINT CBYTE(128) '-128 PRINT CBYTE(129) '127 PRINT CBYTE(255) '-1 PRINT CBYTE(256) '0 and warning "Overflow in constant conversion" PRINT CBYTE(257) '1 and warning "Overflow in constant conversion" PRINT CBYTE(258) '2 and warning "Overflow in constant conversion" PRINT CBYTE(-258) '-2 and warning "Overflow in constant conversion"
According to its documentation, CBYTE accepts floating point numbers and rounds them down by cutting off the decimal part (see FIX).
The function accepts non-numeric arguments, such as strings and user-defined types. FreeBASIC's documentation only states that strings are passed through the VALINT function, so a string of digits is converted to a number before the to-byte conversion. Normal strings are converted to 0.
Gambas does not seem to be that open to non-numeric arguments and issues "Type Mismatch" errors if the value cannot be converted. According to its documentation, a boolean true is converted to 255. Also, it seems that it wraps numeric values into unsigned byte values, like FreeBASIC's CUBYTE, as CBYTE(100000)
returns 160 while FreeBASIC returns -96.
Examples
Comments
Related keywords
Similar keywords
In other languages...
References
- https://gambaswiki.org/wiki/lang/cbyte , last check 2024-03-08
- https://www.freebasic.net/wiki/KeyPgCbyte , last check 2024-03-08