Main Content

typecast

Convert data type without changing underlying data

Description

example

Y= typecast(X,type)converts the bit patterns ofXto the data type specified bytypewithout changing the underlying data.Xmust 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 =uint1665535

上海ow the bit patterns in hexadecimal representation. Converting the data type by usingtypecastdoes not change the underlying data.

formathexX
X =int16ffff
Y = typecast(X,'uint16')
Y =uint16ffff

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

X = int8([77 60 43 26])
X =1x4 int8 row vector77 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 =single3.5411e-23

上海ow the bit patterns in hexadecimal representation. In hexadecimal notation, 1 byte (8 bits) is represented by two digits. Thetypecastfunction rearranges the bit patterns without modifying the data.

formathexX
X =1x4 int8 row vector4d 3c 2b 1a
Y = typecast(X,'single')
Y =single1a2b3c4d

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

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

CastXinto 8-bit unsigned integers usingtypecast. 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 vector1 0 0 0 255 0 0 0 0 1 0 0

The third element ofX, 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 vector0 1 0 0

You can convertYback to 32-bit unsigned integers without changing the underlying data.

X2 = typecast(Y,'uint32')
X2 =1x3 uint32 row vector1 255 256

Compare the output oftypecastand the output ofcastto see the difference between the two functions.

Z = cast(X,'uint8')
Z =1x3 uint8 row vector1 255 255
X2 = cast(Z,'uint32')
X2 =1x3 uint32 row vector1 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. Thetypecastfunction returns the output in little-endian style, combining the four 8-bit segments of the input data to produce two 16-bit segments.

formathexX = uint8([44 55 66 77])
X =1x4 uint8 row vector2c 37 42 4d
Y = typecast(X,'uint16')
Y =1 x2 uint16行向量372c 4d42

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

Y = swapbytes(typecast(X,'uint16'))
Y =1 x2 uint16行向量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 oftypeisntimes larger than the bit size of each element ofX, thenXmust contain a multiple ofnelements to convertXto the data typetype. Otherwise, MATLAB®throws an error.

Tips

  • typecastis different from the MATLABcastfunction in that it does not alter the input data.typecastalways returns the same number of bytes in the outputYas in the inputX. For example, casting the 16-bit integer 1000 touint8withtypecastreturns the full 16 bits in two 8-bit segments (3 and 232), thus keeping the original value (3*256 + 232 = 1000). Thecastfunction, on the other hand, truncates the input value to 255.

  • The format oftypecastoutput can differ depending on the system you use. Some computer systems store data starting with the least significant byte (an ordering calledlittle-endian), while others start with the most significant byte (calledbig-endian). You can use theswapbytesfunction to reverse the byte ordering from little-endian to big-endian (and vice versa).

Extended Capabilities

Version History

Introduced before R2006a

See Also

||