How can i reshape the code as below

조회 수: 2 (최근 30일)
Kaavya N
Kaavya N 2021년 5월 2일
댓글: Kaavya N 2021년 5월 3일
k=[dec2hex(floor(rand * 2 ^ 96),24),dec2hex(floor(rand * 2 ^ 96),24), dec2hex(floor(rand * 2^ 64),16)];
%k generates random 64 characters
%k = 0924668B8FB8700000000000D96089C6C815880000000000EF1A2E75CDB28000
w=reshape(k,4,[ ]);
%w gives 4x16 char array
% '068700D8C800E2C8'
%'96F000998800FED0'
%'28B0006C100017B0'
%'4B8000065000A520'
how can I group 2 characters as one element and make w a 4x8 matrix like
09 8F ....................
24 B8 .....................
66 .........................
8B ...........................
  댓글 수: 2
Image Analyst
Image Analyst 2021년 5월 2일
What is "key"?
Kaavya N
Kaavya N 2021년 5월 2일
sorry its k

댓글을 달려면 로그인하십시오.

채택된 답변

DGM
DGM 2021년 5월 3일
Try this:
k = '0924668B8FB8700000000000D96089C6C815880000000000EF1A2E75CDB28000'
wo = reshape(k(1:2:end),4,[]);
we = reshape(k(2:2:end),4,[]);
w = reshape([wo; we],4,[])
gives
w =
4×16 char array
'098F00D9C800EFCD'
'24B8006015001AB2'
'6670008988002E80'
'8B0000C600007500'
If you need literal spaces in between each byte, that can be done instead:
wo = reshape(k(1:2:end),4,[]);
we = reshape(k(2:2:end),4,[]);
ws = repmat(' ',[4 8]);
w = reshape([wo; we; ws],4,[])
gives
w =
4×24 char array
'09 8F 00 D9 C8 00 EF CD '
'24 B8 00 60 15 00 1A B2 '
'66 70 00 89 88 00 2E 80 '
'8B 00 00 C6 00 00 75 00 '
Note the method of stacking arrays to implement the striping behavior.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 5월 3일
편집: Walter Roberson 2021년 5월 3일
compose("%02x", reshape(sscanf(k,'%2x'),4,[]))
This will return a 4 x 8 string array, not a char array. If you want a 4 x 8 array, then you need to use a cell array or a string array, or you need to convert to decimal. The reshape(sscanf(k,'%2x'),4,[]) part is doing the conversion to decimal and re-ordering of values.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by