typecast
Reinterprets the bytes of a value as another numeric type, without converting the values.
Category: Language Fundamentals > Data Types
Syntax
Y = typecast(X, type)
Returns the bytes of X read as numbers of class type.
Description
Unlike double(x) or uint8(x), which convert values, typecast keeps the memory bits intact and only changes how they are read. For example, the single-precision number 1 has the bit pattern 0x3F800000, so typecast(single(1), 'uint32') is 1065353216.
The total number of bytes must fit evenly in the new type: eight uint8 bytes make one double, but seven do not, and an error is raised. The result follows the byte order of the machine, which is little endian on common hardware.
Input arguments
X- Numeric or logical vector.
type- Target class:
'uint8','int16','single','double', and so on.
Output arguments
Y- Vector with the same bytes as
X. The number of elements changes according to the sizes of the two types.
Example
Reading the same bytes as other types
typecast(single(1), 'uint32')
typecast(uint8([1 0 0 0]), 'uint32')
typecast(uint8([0 0 128 63]), 'single')
typecast(int8([-1 -1]), 'uint16')
Output
ans = uint32
1065353216
ans = uint32
1
ans = single
1
ans = uint16
65535