Convert data types without changing underlying data


Y = typecast(X, type)


Y = typecast(X, type) converts a numeric value in X to the data type specified by type. Input X must be a full, noncomplex, numeric scalar or vector. The type input is a string set to one of the following: 'uint8', 'int8', 'uint16', 'int16', 'uint32', 'int32', 'uint64', 'int64', 'single', or 'double'.

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 were 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 its original value (3*256 + 232 = 1000). The cast function, on the other hand, truncates the input value to 255.

The output of typecast can be formatted differently depending on what system you use it on. Some computer systems store data starting with its most significant byte (an ordering called big-endian), while others start with the least significant byte (called little-endian).


MATLAB issues an error if X contains fewer values than are needed to make an output value.


Example 1

This example converts between data types of the same size:

typecast(uint8(255), 'int8')
ans =

typecast(int16(-1), 'uint16')
ans =

Example 2

Set X to a 1-by-3 vector of 32-bit integers, then cast it to an 8-bit integer type:

X = uint32([1 255 256])
X =
           1         255         256

Running this on a little-endian system produces the following results. Each 32-bit value is divided up into four 8-bit segments:

Y = typecast(X, 'uint8')
Y =
   1   0   0   0  255   0   0   0   0   1   0   0

The third element of X, 256, exceeds the 8 bits that it is being converted to in Y(9) and thus overflows to Y(10):

ans =
    0    1    0    0

Note that length(Y) is equal to 4.*length(X). Also note the difference between the output of typecast versus that of cast:

Z = cast(X, 'uint8')
Z =
    1  255  255

Example 3

This example casts a smaller data type (uint8) into a larger one (uint16). Displaying the numbers in hexadecimal format makes it easier to see just how the data is being rearranged:

format hex
X = uint8([44 55 66 77])
X =
   2c   37   42   4d

The first typecast is done on a big-endian system. The four 8-bit segments of the input data are combined to produce two 16-bit segments:

Y = typecast(X, 'uint16')
Y =
   2c37   424d

The second is done on a little-endian system. Note the difference in byte ordering:

Y = typecast(X, 'uint16')
Y =
   372c   4d42

You can format the little-endian output into big-endian (and vice versa) using the swapbytes function:

Y = swapbytes(typecast(X, 'uint16'))
Y =
   2c37   424d

Example 4

This example attempts to make a 32-bit value from a vector of three 8-bit values. MATLAB issues an error because there are an insufficient number of bytes in the input:

format hex
typecast(uint8([120 86 52]), 'uint32')

Error using typecast
Too few input values to make output type.

Repeat the example, but with a vector of four 8-bit values, and it returns the expected answer:

typecast(uint8([120 86 52 18]), 'uint32')
ans =

Extended Capabilities

See Also

| |

Introduced before R2006a