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

% This function converts an decimal number into corresponding octagonal number.
%  Class of both variables - output and input are "char".
%
% Example:
% hex_2_bin('71253785ae6DEF324534Ff')
% ans =
% 111000100100101001101111000010110101110011011011110111100110010010001010011010011111111
input=char(input-32.*floor((sign(123-input)+sign(input-96))/2));
check1=sign(length(input-48)-sum(sign(abs(input-48+1)+(input-48+1))))+sign(sum(abs(input-48-22)+(input-48-22)));
check2=sign(sum(sign(abs(input-48-9)+(input-48-9))-sign(abs(input-48-16)+(input-48-16))));
if ( check1 || check2 )
    output ='Invalid Number';
else
    if input=='0'
        output='0';
    else
        c=[];
        for i=1:length(input)
            c=[c hex2bin(input(i))]; %#ok<AGROW>
        end
        output=c(find(c-48,1):length(c));
    end
end

Contact us