function s = dec2base(d,base,n)
%DEC2BASE Convert decimal integer to base B string.
%   DEC2BASE(D,B) returns the representation of D as a string in
%   base B.  D must be a non-negative integer smaller than 2^52
%   and B must be an integer between 2 and 36.  
%
%   DEC2BASE(D,B,N) produces a representation with at least N digits.
%
%   Examples
%       dec2base(23,3) returns '212' 
%       dec2base(23,3,5) returns '00212'
%
%   See also BASE2DEC, DEC2HEX, DEC2BIN.

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.8 $  $Date: 1997/11/21 23:47:11 $

% written by Douglas M. Schwarz
% Eastman Kodak Company
% schwarz@kodak.com
% 18 February 1996

error(nargchk(2,3,nargin));

if size(d,2) ~= 1, d = d(:); end

base = floor(base);
if base < 2 | base > 36, error('B must be between 2 and 36.'); end
if base == 2,
  [x,nreq] = log2(max(d));
else
  nreq = ceil(log2(max(d) + 1)/log2(base)); 
end

if nargin == 3
    nreq = max(nreq,1);
    n = max(n,nreq);
    last = n - nreq + 1;
else
    n = max(nreq,1);
    last = 1;
end

s(:,n) = rem(d,base);
while n ~= last
    n = n - 1;
    d = floor(d/base);
    s(:,n) = rem(d,base);
end

symbols = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
s = reshape(symbols(s + 1),size(s));
