Main Content

num2hex

Convert number to hexadecimal equivalent using quantizer object

Description

example

y = num2hex(q,x) converts numeric matrix x into a hexadecimal string returned in y. The attributes of the number are specified by the quantizer object q.

[y1,y2,…] = num2hex(q,x1,x2,…) converts numeric matrices x1, x2, … to hexadecimal strings y1, y2, ….

Examples

collapse all

Use num2hex to convert a matrix of numeric values to hexadecimal representation.

Convert Floating-Point Values

This is a floating-point example using a quantizer object q that has a 6-bit word length and a 3-bit exponent length.

x = magic(3);
q = quantizer('float',[6 3]);
y = num2hex(q,x)
y = 9x2 char array
    '18'
    '12'
    '14'
    '0c'
    '15'
    '18'
    '16'
    '17'
    '10'

Convert Fixed-Point Values

All of the 4-bit fixed-point two's complement numbers in fractional form are given by:

q = quantizer([4 3]);
x = [0.875    0.375   -0.125   -0.625
     0.750    0.250   -0.250   -0.750
     0.625    0.125   -0.375   -0.875
     0.500        0   -0.500   -1.000];
y = num2hex(q,x)
y = 16x1 char array
    '7'
    '6'
    '5'
    '4'
    '3'
    '2'
    '1'
    '0'
    'f'
    'e'
    'd'
    'c'
    'b'
    'a'
    '9'
    '8'

Input Arguments

collapse all

Attributes of the number, specified as a quantizer object.

Numeric values to convert, specified as a scalar, vector, matrix, multidimensional array, or cell array of doubles.

Data Types: double | cell
Complex Number Support: Yes

Output Arguments

collapse all

Hexadecimal strings, returned as a column vector. If x is a cell array containing numeric matrices, then y is returned as a cell array of the same dimension containing hexadecimal strings.

Tips

  • num2hex and hex2num are inverses of each other, except that hex2num returns the hexadecimal values in a column.

Algorithms

  • For fixed-point quantizer objects, the representation is two's complement.

  • For floating-point quantizer objects, the representation is IEEE® Standard 754 style.

    For example, for q = quantizer('double'):

    q = quantizer('double');
    num2hex(q,nan)
    ans =
    
        'fff8000000000000'

    The leading fraction bit is 1, and all the other fraction bits are 0. Sign bit is 1, and exponent bits are all 1.

    num2hex(q,inf)
    ans =
    
        '7ff0000000000000'

    Sign bit is 0, exponent bits are all 1, and all fraction bits are 0.

    num2hex(q,-inf)
    ans =
    
        'fff0000000000000'

    Sign bit is 1, exponent bits are all 1, and all fraction bits are 0.

Version History

Introduced before R2006a