function h = dec2hex(d,n)
%DEC2HEX Convert decimal integer to hexadecimal string.
%   DEC2HEX(D) returns the hexadecimal representation of decimal
%   integer D as a string.  D must be a non-negative integer smaller
%   than 2^52.  
%
%   DEC2HEX(D,N) produces a hexadecimal representation with at least
%   N digits.
%
%   Example
%       dec2hex(2748) returns 'ABC'.
%    
%   See also HEX2DEC, HEX2NUM, DEC2BIN, DEC2BASE.

%   Author: L. Shure
%   Revised by: CMT 1-22-95
%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 5.12 $  $Date: 2000/06/01 15:23:32 $

bits32 = 4294967296;       % 2^32

if any(d < 0), error('D must be a non-negative integer.'); end
if isempty(d), h = ''; return, end
d = d(:); % Make sure d is a column vector.

if nargin==1,
  n = 1; % Need at least one digit even for 0.
end


[f,e] = log2(max(d));
n = max(n,ceil(e/4));

if all(d<bits32),
  h = sprintf(['%0' int2str(n) 'X'],d);
else
  d1 = floor(d/bits32);
  d2 = rem(d,bits32);
  h = sprintf(['%0' int2str(n-8) 'X%08X'],[d1,d2]');
end
h = reshape(h,n,length(d))';
