the given message should be converted into binary 8bit data and at the end of the message a 1024 bits i.e, 1 followed by 1023 zeros must be padded. how can i do this in above coading....
how can i pad 1024 bits of 1 followed by zeros for already existing binary data(k) if my code is?
조회 수: 1 (최근 30일)
이전 댓글 표시
* data = input('enter the data:','s');
bin = double(data);
display(bin);
n=dec2base(bin,16);
display(n);
k = [dec2bin(bin,8)];
display(k);
len_n = length(n);
display(len_n);
tot_len = len_n*4*2;
len_k = length(k);
display(tot_len);
x = 896-tot_len;
y = x/4;
display(x);
display(y);
if len_-n < 896 padarray = [ones(1,1), zeros(1,x-1)]; display(padarray); end
채택된 답변
Stephen23
2014년 10월 12일
편집: Stephen23
2014년 10월 12일
The question does not clearly explain what you are trying to achieve. It would be clearer if you gave exact examples of the data now, and how you want to change it (eg padding, etc).
It seems that you have some existing numeric vector A and wish to pad this with zeros up to a certain length, then you can try:
A = 1:10;
if numel(A)<1024
A(1024) = 0;
end
OR
A = 1:10;
A = [A,zeros(1,1024-numel(A))]
These will pad the data with zeros up until the index that you provide (e.g. 1024). You might also like to look at padarray .
Note that you use the code ones(1,1), which is simply equivalent to 1. Did you mean to create a vector here, eg ones(1024,1) OR ones(1024-len_n) ?
Also the variable data is a string, which you convert to the character codes using double(data). Is this intentional? Otherwise you can convert the value in the string with num2str, or another string parsing function.
댓글 수: 2
Stephen23
2014년 10월 30일
This is because the variable k is not a horizontal vector, unlike the example I gave. You have to use the dimensions of k as inputs to the zeros function. If k is a matrix, one possibility would be (untested):
S = size(k);
C = [k, zeros(S(1),1024-S(2))];
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!