from
Internet Checksum
by Nawaz Satvilkar
Calculates checksum for given stream of data
|
| InternetCheckSum.m |
%Program to generate 16 bit Internet Check Sum
%you can change the length by changing wordSize
clc
wordSize=16;
Din=input('Input the data string of lenght multiple of 16 bit:');
[m n]=size(Din);
% Making block of data
N=n/wordSize;
copy=zeros(N,wordSize);
for i=0:N-1
for j=1:wordSize
copy(i+1,j)=Din(wordSize*i+j);
end
end
copy;
% Finding Sum
checksum=0;
for k=1:N
checksum=checksum+bin2dec(num2str(copy(k,:)));
end
%Decimal to binary
d=checksum;
re=zeros(1,N*wordSize);
for i=1:N*wordSize
re(i)=mod(d,2);
d=d-re(i);
d=d/2;
end
re=fliplr(re);
% Wrap around
wrap=zeros(N,wordSize);
for i=0:N-1
for j=1:wordSize
wrap(i+1,j)=re(wordSize*i+j);
end
end
wrap;
checksumm=0;
for k=1:N
checksumm=checksumm+bin2dec(num2str(wrap(k,:)));
end
% Decimal to binary
d=checksumm;
re=zeros(1,wordSize);
for i=1:wordSize
re(i)=mod(d,2);
d=d-re(i);
d=d/2;
end
re=fliplr(re);
CheckSumToBeTransmitted=~re
% Example1 --Internet checksum for word Fourozan ASCII(466F726F757A616E)HEX is
% (7038)HEX
% Input the data string of lenght multiple of 16 bit:[0 1 0 0 0 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 0 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 0 1 0 1 0 1 1 1 1 0 1 0 0 1 1 0 0 0 0 1 0 1 1 0 1 1 1 0]
%
% CheckSumToBeTransmitted =
%
% Columns 1 through 15
%
% 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0
%
% Column 16
%
% 0
% Example2 --Internet checksum for word SNawaz ASCII(534E6177617A)HEX is
% (E9BF)HEX
|
|
Contact us at files@mathworks.com