function h = num2hex(x) 
%MATLAB Code Generation Library Function

%   Copyright 1984-2014 The MathWorks, Inc.
%#codegen

eml_invariant(nargin > 0, ...
    'MATLAB:minrhs');
eml_invariant(isa(x,'float'), ...
    'MATLAB:num2hex:floatpointInput',class(x));
nx = numel(x);
b = coder.const(coder.internal.indexDivide(coder.internal.float_nbits(class(x)),8));
h = coder.nullcopy(char(zeros(nx,2*b)));
Atoa = int8('a'-'A');
rb = coder.const(reverse_bytes);
for k = 1:nx
    bytes = typecast(x(k),'uint8');
    for j = 0:b-1
        if rb
            h2 = dec2hex(bytes(b-j),2);
        else
            h2 = dec2hex(bytes(j+1),2);
        end
        if h2(1) >= 'A' && h2(1) <= 'F'
            h2(1) = eml_plus(int8(h2(1)),Atoa,'int8','spill');
        end
        if h2(2) >= 'A' && h2(2) <= 'F'
            h2(2) = eml_plus(int8(h2(2)),Atoa,'int8','spill');
        end
        h(k,2*j+1) = h2(1);
        h(k,2*j+2) = h2(2);
    end
end

%--------------------------------------------------------------------------

function rb = reverse_bytes
coder.extrinsic('typecast');
onebytes = coder.const(typecast(1,'uint8'));
rb = coder.const(onebytes(1) == 0);

%--------------------------------------------------------------------------
