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=dec_2_bin(input)
function output=dec_2_bin(input)

% This function converts an decimal number into corresponding binary number.
%  Class of both variables - output and input are "char".
%
% Example:
% dec_2_bin('3423432323563455442374874327') 
% ans =
% 10110000111111001011111111100010000011010011101010111010110011110001101011111101010011010111
if ( sign(length(input-48)-sum(sign(abs(input-48+1)+(input-48+1))))+sign(sum(abs(input-48-9)+(input-48-9))) )
    output ='Invalid Number';
else
    if input=='0'
        output='0';
    else
        for i=1:length(input)
            array(i)=(input(i))-48; %#ok<AGROW>
        end
        b=dec2bin2(array);
        for i=1:length(b)
            output(i)=int2str(b(i)); %#ok<AGROW>
        end
    end
end

Contact us