MATLAB Program for Converting a Text File to Stream of Bits

What is a text file specifically? A sequence of character.
And how these characters can be visualized in terms of a number? By their ASCII equivalent.
And the ASCII equivalent code can be converted to binary format to get bit stream.
Refer WikiPedia page of ASCII to get a detailed information about ASCII codes. Or search over the internet with the term "ASCII".

The following code will be a blend of MATLAB's data file handling (accessing & reading the file saved in disk), some processing & interconversion among data-types (typecasting to be more specific).

The resultant 1D array will be a continuous stream of  1s & 0s depending on the text in the text file.

MATLAB code for Converting a Text File to Stream of Bits

clc 
clear all 
close all
[f_name,path] = uigetfile({'*.txt';'*.*'},'Select the Text (.txt) file'); 

%above line will let you to browse for the file using a GUI window
path=[path f_name]; %building the path of the text file
A = importdata(path);
%import a text data as a structure
x=A(1);
for i=2:size(A,1)
x=strcat(x,'\n');
%preserving a line brake like this, it will be helpful while reconstruction
x=strcat(x, A(i));
end
%this loop is to concatenate all the characters in a continuous sequence
x=x{1}; %converting structure to a character array
c=dec2bin(x,8); %create a cell of x character & 8bit
y=c(1,:);
%for a continoues bit stream
for i=2:size(c,1)
y=strcat(y,c(i,:));
end
for i=1:size(y,2)
z(i)=double(y(i));
end
z(find(z(:)==48))=0;
z(find(z(:)==49))=1;
%z is a bitstream now
disp('The Resultant Bit Stream is:');

disp(z);

Sample Text & the output bit stream using above MATLAB code to convert a text file to bit stream.

Suppose the text file is with the name : test.txt contains the following text,
Andhra pradesh
Orissa
rajashthan
Uttrakhand
bihar
Arunachal pradesh
Tamil Nadu
Gujrat
Andaman and nicobar
Madhya Pradesh

The resultant bit stream after the execution is:
010000010110111001100100011010000111001001100001001000000111000001110010011
000010110010001100101011100110110100001011100011011100100111101110010011010
010111001101110011011000010101110001101110011100100110000101101010011000010
111001101101000011101000110100001100001011011100101110001101110010101010111
010001110100011100100110000101101011011010000110000101101110011001000101110
001101110011000100110100101101000011000010111001001011100011011100100000101
110010011101010110111001100001011000110110100001100001011011000010000001110
000011100100110000101100100011001010111001101101000010111000110111001010100
011000010110110101101001011011000010000001001110011000010110010001110101010
1110001101110010001110111010101101010011100100110000101110100010111000110111
0010000010110111001100100011000010110110101100001011011100010000001100001011
0111001100100001000000110111001101001011000110110111101100010011000010111001
0010111000110111001001101011000010110010001101000011110010110000100100000010
10000011100100110000101100100011001010111001101101000





1 comment:

  1. can you give the code for reversing the above action.

    ReplyDelete