Skip to content
Paul Squires edited this page Oct 19, 2026 · 2 revisions

Home › Converting Data Types › CByte

CByte

Converts a number or string to a Byte, rounding to the nearest; no overflow check.

Syntax

Declare Function CByte ( ByVal expression As datatype ) As Byte
Type typename
Declare Operator Cast ( ) As Byte
End Type

Parameters

  • expression
  • datatype
  • typename

Description

The CByte function rounds off the decimal part and returns a 8-bit Byte value. The function does not check for an overflow, and results are undefined for values which are less than -128 or larger than 127.

The name can be explained as 'Convert to Byte'.

If the argument is a string expression, it is converted to numeric by using ValInt.

Return Value

A Byte value.

Remarks

Usage

result = CByte( numeric expression )

result = CByte( string expression )

result = CByte( user defined type )

Dialect Differences

  • Not available in the -lang qb dialect unless referenced with the alias __Cbyte.

Differences from QB

  • New to FreeBASIC

See also

Examples

' Using the CBYTE function to convert a numeric value

'Create an BYTE variable

Dim numeric_value As Byte

'Convert a numeric value

numeric_value = CByte(-66.30)

'Print the result, should return -66

Print numeric_value

Sleep

Category: Converting Data Types
Kind: function


Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.

Clone this wiki locally