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

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

Contact us