How can I use function 'typecast' to convert triplets of 8-byte variables to int24?

4 Ansichten (letzte 30 Tage)
It is provided triplets of 8 bytes which are sought to be converted to 'int24' datatype. Consider an array 'A' that contains such kind of triplets, namely,
 
A = uint8([1 2 3 4 5 6]);
 
How can I use function 'typecast' to convert triplets of a 8 bytes, namely 'A(1:3)' and 'A(4:6)' in this case, to 'int24' datatype?

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 27 Jul. 2021
It is not possible to use function 'typecast' to convert a variable to 'int24', see the following documentation page accordingly,
However, one can add 0 padding into the corresponding triplets and have them converted to 'int32' using function 'typecast' which is the next higher datatype to datatype 'int24'. This workflow is demonstrated using the following code snippet,
% Raw example data
A = uint8([1 2 3 4 5 6]);
% Reshape such that we can then easily add a row of zeros to all of them
A = reshape(A,3,[]);
% Add the padding zeros
A = [A;zeros(1,size(A,2))];
% One typecast for the whole flat array again
typecast(A(:),'uint32')

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Produkte


Version

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by