cmn_oct2bin.m 1.72 KB
Newer Older
Ben Wojtowicz's avatar
Ben Wojtowicz committed
1
%
2
% Copyright 2011-2012 Ben Wojtowicz
Ben Wojtowicz's avatar
Ben Wojtowicz committed
3 4
%
%    This program is free software: you can redistribute it and/or modify
5
%    it under the terms of the GNU Affero General Public License as published by
Ben Wojtowicz's avatar
Ben Wojtowicz committed
6 7 8 9 10 11
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    This program is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
%    GNU Affero General Public License for more details.
Ben Wojtowicz's avatar
Ben Wojtowicz committed
13
%
14
%    You should have received a copy of the GNU Affero General Public License
Ben Wojtowicz's avatar
Ben Wojtowicz committed
15 16 17 18 19 20 21 22 23 24 25 26
%    along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
% Function:    cmn_oct2bin
% Description: Converts an array of octal numbers
%              to an array of binary arrays
% Inputs:      oct      - Array of octal numbers
%              num_bits - Number of bits per octal
%                         number
% Outputs:     array    - Array of binary arrays
% Spec:        N/A
% Notes:       None
% Rev History: Ben Wojtowicz 11/22/2011 Created
27
%              Ben Wojtowicz 01/29/2012 Fixed license statement
Ben Wojtowicz's avatar
Ben Wojtowicz committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
%
function [array] = cmn_oct2bin(oct, num_bits)
    [junk, num_oct] = size(oct);

    for(n=1:num_oct)
        % Convert whole digits
        tmp = oct(n);
        idx = num_bits;
        for(m=1:floor(num_bits/3))
            dig                = mod(tmp, 10);
            array(n,idx-2:idx) = cmn_dec2bin(dig, 3);
            tmp                = floor(tmp/10);
            idx                = idx - 3;
        endfor

        % Convert non-whole digits
        dig             = mod(tmp, 10);
        array(n, 1:idx) = cmn_dec2bin(dig, mod(num_bits, 3));
    endfor
endfunction