Name
CByte Function
Named Arguments
No
Syntax
CByte(expression)-
expression Use: Required
Data Type: Numeric or String
A string or numeric expression that evaluates to a number between 0 and 255
Return Value
expression converted to Byte data type
Description
Converts expression to a Byte data type
Rules at a Glance
If the expression to be converted is a string, the string must be capable of conversion to a numeric expression; this can be checked using the
IsNumericfunction.If
expressionevaluates to less than 0 or more than 255, a runtime error is generated.If the value of
expressionis not a whole number,CByterounds the number prior to conversion.
Example
If IsNumeric(sMyNumber) Then
If val(sMyNumber) >= 0 and val(sMyNumber) <= 255 Then
BytMyNumber = CByte(sMyNumber)
End If
End IfProgramming Tips and Gotchas
Check that the value you pass to
CByteis neither negative nor greater than 255.Use
IsNumericto ensure that the value passed toCBytecan be converted to a numeric expression.When using
CByteto convert floating point numbers, fractional values up to but not including .5 are rounded down, while values above but not including .5 are rounded up. Values whose fractional component is exactly equal to .5 are rounded up if their integral component is odd and down if their integral component is even.The
CBytefunction converts an expression to an unsigned byte data type. To convertexpressionto a signed byte data type, create an instance of the SByte class and call its Parse method. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access