Converting VBScript's CByte Function

Windows PowerShell

Definition: Returns an expression that has been converted to a Variant of subtype Byte.


What do you mean, “Windows PowerShell is great and all, but there’s probably no way to convert a variable to the byte data type?" As it turns out, that's about as easy a conversion as you could hope to make. For example, the first of these two commands assigns a string value to the variable $a; the second command then converts $a to the byte data type (which, incidentally, consists of integers from 0 to 255):

$a = "11.45"
$a = [byte] $a

When you run these two commands and then use the GetType() method to determine the data type of $a you should get the following:

IsPublic IsSerial Name
-------- -------- ----
True     True     Byte

Incidentally, the value of $a will now be 11.

Return to the VBScript to Windows PowerShell home page