Type Cast Function

Owning Palette: Data Manipulation Functions

Requires: Base Development System

Casts x to the data type, type, by flattening it and unflattening it using the new data type. If the function must reinterpret data instead of transforming it, LabVIEW uses a temporary buffer. This function has the risk of reinterpreting data. To preserve the data value while converting the data, use the Coerce To Type function instead.

Note  You can also use the Conversion VIs and functions to convert data types.

Details  

 Add to the block diagram  Find on the palette
type is the data type to which you want to convert the data. Wire any constant or control to type to set the data type to which you want to convert the data. LabVIEW ignores any data in the constant or control you wire to type.
x specifies the data you want to convert.

This input accepts any data type.
*(type *) &x is the value provided in x and converted to the same data type as type.

Type Cast Details

Choosing between the Type Cast Function and the Coerce To Type Function

The Coerce To Type function also converts data to a desired data type. Unlike the Coerce To Type function, the Type Cast function allows conversion between incompatible data types by flattening and unflattening the input data, which allows for radical reinterpretation of the data. When the desired data type requires a different memory size than the input data, the Type Cast function may reinterpret the input data. The Coerce To Type function allows only conversion between compatible data types to preserve the value of the input data, similar to what a coercion dot does. You also can use the Coerce To Type function to convert a type definition or rename data on the wire, such as a user event refnum. NI recommends that you use the Coerce To Type function instead of the Type Cast function to avoid reinterpreting the data.

Effects of Mismatching the Sizes of X and Type

This function can generate unexpected data if x and type are not the same size. If x requires more bits of storage than type, this function uses the upper bytes of x and discards the remaining lower bytes. If x is of a smaller data type than type, this function moves the data in x to the upper bytes of type and fills the remaining bytes with zeros. For example, an 8-bit unsigned integer with value 1 type cast to a 16-bit unsigned integer results in a value of 256.

Type Casting Arrays

You can use this function with an array of scalars or an array of clusters of scalars. For example, if you typecast an array of four 16-bit integers to an array of 32-bit integers, the output array contains two elements, each formed from the bits of pairs of elements from the input array. If the input array does not contain enough bytes to form a whole number of output elements, LabVIEW omits the final elements of the input array.

Type Casting References

If you want to upcast or downcast references, use the To More Generic Class function and the To More Specific Class function instead of the Type Cast function. The Type Cast function does not provide error checking, but the To More Generic Class function and the To More Specific Class function do. The To More Specific Class function has error in and error out parameters. The To More Generic Class function breaks the wire at edit time when you wire a reference to an incompatible target class.

Type Casting Boolean Data from Early Versions of LabVIEW

If x might contain Boolean data created with LabVIEW 4.x or earlier, right-click this function and select Convert 4.x Data from the shortcut menu to convert the data to a format that is readable by LabVIEW 5.0 and later. In Convert 4.x Data mode, this function interprets x as if the data is stored in LabVIEW 4.x data storage layout and displays the icon for this function with a red 4.x on it. LabVIEW 4.x and earlier stores Boolean data in two bytes unless the data is in an array, in which case LabVIEW stores each Boolean element in a single bit. LabVIEW 5.0 and later stores Boolean values in a single byte, regardless of whether it is in an array. National Instruments recommends reworking any application that uses the Convert 4.x Data mode as a long term solution.