Name
CByte Function
Syntax
CByte(expression)-
expression Use: Required
Data Subtype: Numeric or String
A string or numeric expression that evaluates between 0 and 255.
Return Value
expression converted to a variant with a
subtype of Byte.
Description
Converts expression to a Variant with a
subtype of Byte. The byte subtype is the smallest data storage device
in VBScript. Being only one byte in length, it can store unsigned
numbers between
and 255.
Rules at a Glance
If
expressionis a string, the string must be capable of being treated as a number.If
expressionevaluates to less than or more than 255, an overflow error is generated.If
expressionisn’t 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 & Gotchas
Check that the value you pass to
CByteis neither negative nor greater than 255.Use
IsNumericto insure the value passed toCBytecan be converted to a numeric expression.When using
CByteto convert floating point numbers, fractional values up to but not including 0.5 are rounded down, while values of 0.5 and above are rounded up.
See Also
| IsNumeric Function |
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