Creating a hex array with 4 digit blocks from an array of integers
이전 댓글 표시
I am using R2013 and need to convert a large decimal array into hex stings.
The decimal numbers can be any positive integer, and I need to convert each number, then display each as a minimum two byte string with preceding zeros (with more bytes shown as the number increases).
e.g. decimal "1" should show up as '0x0001',
decimal "69876" should show up as '0x0001 0x10F4'
I created a rather cumbersome loop that does this perfectly, but with large arrays it takes too long.
I can get part way to a non-loop implementation with this code:
DecArray = [1 2 4 69876];
cellstr(dec2hex(reshape(DecArray,[],1), 4));
ans =
'00001'
'00002'
'00004'
'110F4'
But as you see the outputs
1) always have the same number of digits, and
2) aren't broken into 2 Byte blocks
Is there any way to go from these results to the ones I described earlier without another loop?
댓글 수: 5
Walter Roberson
2017년 1월 21일
How would the output be interpreted? If you have
0x0001 0x10F4
as output, then how would someone or a program reading it know whether that should be 1 followed by 4340 or should be interpreted as the pair 69876 ?
Walter Roberson
2017년 1월 22일
They can read hex, but only if it is broken up into words in that particular format??
Walter Roberson
2017년 1월 22일
I dunno. It just seems unlikely to me that the data could vary between 16 and 32 bits on the same plot.
채택된 답변
추가 답변 (1개)
Guillaume
2017년 1월 21일
DecArray = [1 2 4 69876]';
h = arrayfun(@dec2hex, DecArray, 'UniformOutput', false);
cellfun(@(s) strcat('0x', cellstr(reshape([repmat('0', 1, mod(-numel(c), 4)), c], 4, [])')), d, 'UniformOutput', false)
댓글 수: 3
Art
2017년 1월 21일
Walter Roberson
2017년 1월 22일
편집: Walter Roberson
2017년 1월 22일
d should be h
Art
2017년 1월 22일
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!