result = Cast(val, sign, type)
result
will not really be a variable of the type
you have cast it to, but it will just be clipped to the boundaries of the
variable type you specify.
However, this function can come in quite handy when it comes to do signed and unsigned conversions. You might want to know which number you get when you want to convert 41234 to a signed short value. You can use this function to do that.
Cast()
accepts three arguments: The first argument is the value that
shall be cast, the second argument specifies whether or not you want
a signed value and must be either True
(= cast to signed) or False
(= cast to unsigned). The last argument finally specifies the type
the value shall be cast to. This can be #INTEGER
(32-bit), #SHORT
(16-bit) or #BYTE
(8-bit).
True
(signed cast) or False
(unsigned cast)#INTEGER
, #SHORT
or #BYTE
)Print(Cast(41234, TRUE, #SHORT))The code above casts the number 41234 to a signed short (16-bit) value and prints the result which is -(2^16-41234) = -24302.