How can I use function 'typecast' to convert triplets of 8-byte variables to int24?
6 views (last 30 days)
Show older comments
MathWorks Support Team
on 27 Jul 2021
Answered: MathWorks Support Team
on 27 Jul 2021
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?
Accepted Answer
MathWorks Support Team
on 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')
0 Comments
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!