Main Content


Convert IEEE hexadecimal format to double-precision number



X = hex2num(hexStr) converts hexStr to the double-precision floating-point number that it represents. The input argument hexStr has up to 16 characters representing a number in its IEEE® format using hexadecimal digits. If hexStr has fewer than 16 digits, then hex2num pads hexStr with zeros to the right.

Unlike the hex2dec function, hex2num can convert inputs that represent floating-point values with fractional parts. Also, hex2num handles NaN and Inf value and denormal numbers correctly.

If hexStr represents multiple numbers, then hex2dec returns the numbers as a column vector of double-precision numbers.


collapse all

Create a character vector that represents a double-precision number in its IEEE® format, using hexadecimal digits.

hexStr = '400921fb54442d18';

To convert it to the number that it represents, use the hex2num function. If hexStr represents a number with a fractional part, then hex2num converts it correctly.

X = hex2num(hexStr)
X = 3.1416

You can convert X back to its hexadecimal representation using the num2hex function.

ans = 

Create a character vector that represents a negative number. Then convert it.

hexStr = 'BFF';
X = hex2num(hexStr)
X = -1

Convert a representation of NaN.

ans = NaN

Convert a representation of Inf.

ans = Inf

Create a string array that represents multiple numbers in their IEEE® format, using hexadecimal digits.

hexStr = ["4005bf0b0d7a3c06" "3fc189374bc6a7f0" "402c"]
hexStr = 1x3 string
    "4005bf0b0d7a3c06"    "3fc189374bc6a7f0"    "402c"

Convert hexStr and return it as an array of double-precision numbers.

X = hex2num(hexStr)
X = 1×3

    2.7183    0.1370   14.0000

Input Arguments

collapse all

Text representing hexadecimal numbers, specified as a character array, cell array of character vectors, or string array.

  • If hexStr is a cell array of character vectors or a string array, then hex2num converts each element to a number.

  • If hexStr is a character array, then hex2num converts each row to a number.

Extended Capabilities

Version History

Introduced before R2006a