%% Convert 32-Bit Integer to 8-Bit Integer
% Create a 1-by-3 vector of 32-bit unsigned integers.

X = uint32([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')

% 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)

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

X2 = typecast(Y,'uint32')

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

Z = cast(X,'uint8')
X2 = cast(Z,'uint32')

%
% Copyright 2019 The MathWorks, Inc.