Main Content

typecast

Convert data type without changing underlying data

Description

example

Y = typecast(X,type) converts the bit patterns of X to the data type specified by type without changing the underlying data. X must be a full, noncomplex, numeric scalar or vector.

Examples

collapse all

Convert an integer to an unsigned integer of the same storage size.

X = int16(-1)
X = int16
    -1
Y = typecast(X,'uint16')
Y = uint16
    65535

Show the bit patterns in hexadecimal representation. Converting the data type by using typecast does not change the underlying data.

format hex
X
X = int16
   ffff

Y = typecast(X,'uint16')
Y = uint16
   ffff

Create a 1-by-4 vector of 8-bit integers.

X = int8([77 60 43 26])
X = 1x4 int8 row vector

   77   60   43   26

Convert the four 8-bit integers, which use 4 bytes (32 bits) of storage, to a single-precision number that also uses 4 bytes of storage.

Y = typecast(X,'single')
Y = single
    3.5411e-23

Show the bit patterns in hexadecimal representation. In hexadecimal notation, 1 byte (8 bits) is represented by two digits. The typecast function rearranges the bit patterns without modifying the data.

format hex
X
X = 1x4 int8 row vector

   4d   3c   2b   1a

Y = typecast(X,'single')
Y = single
   1a2b3c4d

Create a 1-by-3 vector of 32-bit unsigned integers.

X = uint32([1 255 256])
X = 1x3 uint32 row vector

     1   255   256

Cast X into 8-bit unsigned integers using typecast. Each 32-bit value is divided into four 8-bit segments. Running this code on a little-endian system produces the following results.

Y = typecast(X,'uint8')
Y = 1x12 uint8 row vector

     1     0     0     0   255     0     0     0     0     1     0     0

The third element of X, 256, exceeds the maximum value that 8 bits can hold. The converted value in Y(9) thus overflows to Y(10).

Y(9:12)
ans = 1x4 uint8 row vector

   0   1   0   0

You can convert Y back to 32-bit unsigned integers without changing the underlying data.

X2 = typecast(Y,'uint32')
X2 = 1x3 uint32 row vector

     1   255   256

Compare the output of typecast and the output of cast to see the difference between the two functions.

Z = cast(X,'uint8')
Z = 1x3 uint8 row vector

     1   255   255

X2 = cast(Z,'uint32')
X2 = 1x3 uint32 row vector

     1   255   255

Casts integers from a smaller data type (uint8) into a larger one (uint16). Use hexadecimal representation to show the rearrangement of the bit patterns. The typecast function returns the output in little-endian style, combining the four 8-bit segments of the input data to produce two 16-bit segments.

format hex
X = uint8([44 55 66 77])
X = 1x4 uint8 row vector

   2c   37   42   4d

Y = typecast(X,'uint16')
Y = 1x2 uint16 row vector

   372c   4d42

You can convert the little-endian output to big-endian (and vice versa) using the swapbytes function.

Y = swapbytes(typecast(X,'uint16'))
Y = 1x2 uint16 row vector

   2c37   424d

Input Arguments

collapse all

Input array, specified as a scalar or vector.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

New data type, specified as 'single', 'double', 'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', or 'uint64'.

If the bit size of type is n times larger than the bit size of each element of X, then X must contain a multiple of n elements to convert X to the data type type. Otherwise, MATLAB® throws an error.

Tips

  • 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).

Extended Capabilities

Version History

Introduced before R2006a

See Also

| |