Convert data type without changing underlying data
typecast
is different from the MATLAB
cast
function in that it does not alter
the input data. typecast
always returns the same number of bytes in
the output Y
as in the input X
. For example, casting
the 16-bit integer 1000 to uint8
with typecast
returns the full 16 bits in two 8-bit segments (3 and 232), thus keeping the original
value (3*256 + 232 = 1000). The cast
function, on the other hand,
truncates the input value to 255.
The format of typecast
output can differ depending on the system
you use. Some computer systems store data starting with the least significant byte (an
ordering called little-endian), while others start with the most
significant byte (called big-endian). You can use the swapbytes
function to reverse the byte ordering from little-endian to big-endian (and vice
versa).