matlab中的十进制到二进制转换

时间:2013-03-25 16:56:53

标签: matlab

从文件派生的文本的十进制到二进制的转换是

temp=textread('E:\one.txt', '%1s', 'whitespace', '');  
text = char(temp);                                                           
y = zeros(length(text)*8,1);                                           
for n = 1:1:length(text)  
a=abs(text(n));                                                              
f = 8*(n-1)+1;                                                          
y(f:f+7,1)=(de2bi(a,8))';                                                   
end
disp('THE MAGNITUDE OF THE TEXT IS =');
disp(a);
disp(f);
x=y';
disp('THE BINARY BITS ARE');
disp(x);

如果文件中包含“1”,则输出此程序

THE MAGNITUDE OF THE TEXT IS =
49

 1

THE BINARY BITS ARE
 1     0     0     0     1     1     0     0

如果x的位数是8位,那么我想在变量中显示前3位,在另一个变量中保留5位 我想在matlab中使用这个程序。

eg x=00110011
a=001
b=10011 

编码程序

clc;
clear all;
temp=textread('E:\one.txt', '%1s', 'whitespace', '');  
text = char(temp);                                                               
y = zeros(length(text)*8,1);                                                
for n = 1:1:length(text)  
    a=abs(text(n));                                                                 
    f = 8*(n-1)+1;                                                                    
    y(f:f+7,1)=(de2bi(a,8))';                                            
end
disp('THE MAGNITUDE OF THE TEXT IS =');
disp(a);
disp(f);
x=y';
disp('THE BINARY BITS ARE');
disp(x);
z=length(x);
savefile='D:\mat\z.mat';
save (savefile,'z','-MAT');
disp('TOTAL NUMBER OF BITS =');
disp(z);
bk=input('ENTER THE NUMBER OF ROWS =');
savefile='D:\mat\bk.mat';
save (savefile,'bk','-MAT');
c=z/bk;
savefile='D:\mat\c.mat';
save (savefile,'c','-MAT');
k=1;
for i=1:bk
    for j=1:c
        m(i,j)=x(k);
        k=k+1;
    end
end
%disp(m(i,j));
disp('THE MESSAGE BITS ARE ');
disp(m);
savefile='D:\mat\m.mat';
save (savefile,'m','-MAT');
m_tot=(size(m,1)*size(m,2));
savefile='D:\mat\m_tot.mat';
save (savefile,'m_tot','-MAT');
savefile='D:\mat\r1.mat';
r1=[randperm(bk),randperm(bk)];
save (savefile,'r1','-MAT');
disp(r1);
savefile='D:\mat\r2.mat';
r2=[randperm(bk),randperm(bk)];
save (savefile,'r2','-MAT');
disp(r2);
savefile='D:\mat\f(1).mat';
f1= randint(1,1,[1,bk]);
save (savefile,'f1','-MAT');
savefile='D:\mat\en.mat';
en(1,:)=m(f1,:);
save (savefile,'en','-MAT');
disp('DIRECTLY ASSIGNED BLOCK IS');
disp(f1);
for w=1:(length(r1))
    en(w+1,:)=xor((m(r1(w),:)),(m(r2(w),:)));
    disp('THE EXORED BLOCKS ARE= ');
    disp(r1(w));
    disp(r2(w));
end
disp('THE ENCODED BITS ARE');
disp(en);
en_tot=(size(en,1)*size(en,2));
disp('tot no of encoded bits');
disp(en_tot);
save (savefile,'en_tot','-MAT');
savefile='D:\mat\en_tot.mat';

变量en应根据与变量x相同的跳数进行分割。

2 个答案:

答案 0 :(得分:0)

只需切片阵列:

disp(x(1:3));
disp(x(4:end));

答案 1 :(得分:0)

试试这个:

%After computing "x", the double array, as required...
d = input('Enter the length of the first sub-array: ');
a = x(1:d)
b = x(d+1:end)

例如:

>> 
temp='1';  
text = char(temp);                                                           
y = zeros(length(text)*8,1);                                           
for n = 1:1:length(text)  
a=abs(text(n));                                                              
f = 8*(n-1)+1;                                                          
y(f:f+7,1)=(de2bi(a,8))';                                                   
end
disp('THE MAGNITUDE OF THE TEXT IS =');
disp(a);
disp(f);
x=y';
disp('THE BINARY BITS ARE');
disp(x);
%After computing "x", the double array, as required...
d = input('Enter the length of the first sub-array: ');
a = x(1:d)
b = x(d+1:end)

结果将是:

THE MAGNITUDE OF THE TEXT IS =
    49

     1

THE BINARY BITS ARE
     1     0     0     0     1     1     0     0

Enter the length of the first sub-array: 3

a =

     1     0     0


b =

     0     1     1     0     0

我仍然不确定该程序应该实现的目标。

修改

根据更改要求的新代码:

>> 
temp='1';  
text = char(temp);                                                           
y = zeros(length(text)*8,1);                                           
for n = 1:1:length(text)  
    a=abs(text(n));                                                              
    f = 8*(n-1)+1;                                                          
    y(f:f+7,1)=(de2bi(a,8))';                                                   
end
disp('THE MAGNITUDE OF THE TEXT IS =');
disp(a);
disp(f);
x=y';
disp('THE BINARY BITS ARE');
disp(x);
%After computing "x", the double array, as required...
d = input('Enter the hop-count vector: ');
for i=2:length(d)
    d(i) = d(i) + d(i-1);
end
d = [0, ceil((d./d(end))*length(x))];
disp('The resultant split up is:')
for i=2:length(d)
    disp(x((d(i-1)+1):d(i)));
end

结果将是:

THE MAGNITUDE OF THE TEXT IS =
    49

     1

THE BINARY BITS ARE
     1     0     0     0     1     1     0     0

Enter the hop-count vector: [3 2 3]
The resultant split up is:
     1     0     0

     0     1

     1     0     0