MATLAB Answers

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

5 ビュー (過去 30 日間)
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?


MathWorks Support Team
MathWorks Support Team 2021 年 7 月 27 日
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
  1 件のコメント
Lasse Boenecke
Lasse Boenecke 2021 年 9 月 19 日 9:44
This does not work with int24 since that datatype is signed, correct?


その他の回答 (0 件)






Community Treasure Hunt

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

Start Hunting!

Translated by