typecast
Convert data type without changing underlying data
Syntax
Description
Examples
Convert Integer to Unsigned Integer of Same Storage Size
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 usingtypecast
does not change the underlying data.
formathexX
X =int16ffff
Y = typecast(X,'uint16')
Y =uint16ffff
Convert 8-Bit Integer to Single Precision
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. Thetypecast
function rearranges the bit patterns without modifying the data.
formathexX
X =1x4 int8 row vector4d 3c 2b 1a
Y = typecast(X,'single')
Y =single1a2b3c4d
Convert 32-Bit Integer to 8-Bit Integer
Create a 1-by-3 vector of 32-bit unsigned integers.
X = uint32([1 255 256])
X =1x3 uint32 row vector1 255 256
CastX
into 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 convertY
back to 32-bit unsigned integers without changing the underlying data.
X2 = typecast(Y,'uint32')
X2 =1x3 uint32 row vector1 255 256
Compare the output oftypecast
and the output ofcast
to 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
Convert 8-Bit Integer to 16-Bit Integer
Casts integers from a smaller data type (uint8
) into a larger one (uint16
). Use hexadecimal representation to show the rearrangement of the bit patterns. Thetypecast
function 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 theswapbytes
function.
Y = swapbytes(typecast(X,'uint16'))
Y =1 x2 uint16行向量2c37 424d
Input Arguments
X
—Input array
scalar|vector
Input array, specified as a scalar or vector.
Data Types:single
|double
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
type
—New data type
'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 oftype
isn
times larger than the bit size of each element ofX
, thenX
must contain a multiple ofn
elements to convertX
to the data typetype
. Otherwise, MATLAB®throws an error.
Tips
typecast
is different from the MATLABcast
function in that it does not alter the input data.typecast
always returns the same number of bytes in the outputY
as in the inputX
. For example, casting the 16-bit integer 1000 touint8
withtypecast
returns the full 16 bits in two 8-bit segments (3 and 232), thus keeping the original value (3*256 + 232 = 1000). Thecast
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 calledlittle-endian), while others start with the most significant byte (calledbig-endian). You can use theswapbytes
function to reverse the byte ordering from little-endian to big-endian (and vice versa).
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
The value of the data type argument must be lowercase.
When you use
typecast
with inherited input port data types inMATLAB Functionblocks, the software can throw a size error. To avoid this error, specify the block input port data types explicitly.Integer input or result classes must map directly to a C type on the target hardware.
The input must be a variable-length vector or a fixed-size vector. SeeVariable-Sizing Restrictions for Code Generation of Toolbox Functions(MATLAB Coder).
The output vector always has the same orientation as the input vector.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
The value of the data type argument must be lowercase.
When you use
typecast
with inherited input port data types inMATLAB Functionblocks, the software can throw a size error. To avoid this error, specify the block input port data types explicitly.Integer input or result classes must map directly to a C type on the target hardware.
The input must be a variable-length vector or a fixed-size vector.
The output vector always has the same orientation as the input vector.
Thread-Based Environment
Run code in the background using MATLAB®backgroundPool
or accelerate code with Parallel Computing Toolbox™ThreadPool
.
This function fully supports thread-based environments. For more information, seeRun MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, seeRun MATLAB Functions on a GPU(Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, seeRun MATLAB Functions with Distributed Arrays(Parallel Computing Toolbox).
Version History
Introduced before R2006a
Apri esempio
如果dispone di una versione modificata di questo esempio. Desideri aprire questo esempio con le tue modifiche?
Comando MATLAB
Hai fatto clic su un collegamento che corrisponde a questo comando MATLAB:
Esegui il comando inserendolo nella finestra di comando MATLAB. I browser web non supportano i comandi MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)