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

% This function converts an binary number into corresponding octagonal number.
%  Class of both variables - output and input are "char".
%
% Example:
% bin_2_oct('0100101111100100110010010101001010010010101111010011101010010101001') 
% ans =
% 4574462251222572352251
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 input=='0'
        output='0';
    else
        if(rem(length(input),3)~=0)
            for i=1:3-rem(length(input),3)
                input=['0' input]; %#ok<AGROW>
            end
        end
        [m n]=size(input);
        b=reshape(input,3,m*n/3)';
        output=[];
        for i=1:m*n/3
            output=[output bin2oct(b(i,:))]; %#ok<AGROW>
        end
    end
    output=output(find(output-48,1):length(output));
end

Contact us