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 view (last 30 days)
Show older comments
* 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
Accepted Answer
Stephen23
on 12 Oct 2014
Edited: Stephen23
on 12 Oct 2014
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 Comments
Stephen23
on 30 Oct 2014
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))];
More Answers (0)
See Also
Categories
Find more on Logical 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!