how can i pad 1024 bits of 1 followed by zeros for already existing binary data(k) if my code is?

4 vues (au cours des 30 derniers jours)
* 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
  1 commentaire
yogya
yogya le 12 Oct 2014
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....

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 12 Oct 2014
Modifié(e) : Stephen23 le 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 commentaires
yogya
yogya le 12 Oct 2014
thank you for helping me. but when i try to run this command in my coad it showing error as ??? Error using ==> horzcat CAT arguments dimensions are not consistent.
Error in ==> prog at 24
C = [k, zeros(1,1024-numel(k))];
what does it mean , how can i correct it
and as i need the binary values of the message i have used double(data) insted of num2str...
Stephen23
Stephen23 le 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))];

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by