Code covered by the BSD License  

Highlights from
NUMBER SYSTEMS/ BASE CONVERTER

image thumbnail

NUMBER SYSTEMS/ BASE CONVERTER

by

 

13 Aug 2006 (Updated )

Convert a number between different number systems-hex,oct,bin,dec.

output=bin_2_dec(input)
function output=bin_2_dec(input)

% This function converts an binary number into corresponding decimal number.
%  Class of both variables - output and input are "char".
%
% Example:
% bin_2_dec('0100101111100100110010010110100100101001010010010101111010011101010010101001') 
% ans =
% 22399866065043652727977
if ( sign(length(input-48)-sum(sign(abs(input-48+1)+(input-48+1))))+sign(sum(abs(input-48-1)+(input-48-1))) ) %#ok<BDLOG>
    output ='Invalid Number';
else
    if length(input)==1
        output=input;
    else
        a=str2num(input); %#ok<ST2NM>
        a=fliplr(a);
        b(1)=a(1);
        for i=2:length(a)
            gopi=str2num(powr2(i-1))*a(i); %#ok<ST2NM>
            gopi2=array_adder(b,gopi);
            b=gopi2;
        end
        for i=1:length(gopi2)
            if(gopi2(i)~=0)
                got=i;
                break;
            end
        end
        for i=1:length(gopi2)-got+1
            gopi3(i)=gopi2(i+got-1); %#ok<AGROW>
        end
        for i=1:length(gopi3)
            output(i)=int2str(gopi3(i)); %#ok<AGROW>
        end
    end
end
end

Contact us