Convert text representation of hexadecimal number to decimal number



D = hex2dec(hexStr) converts hexStr to a decimal number and returns it. The input argument hexStr is text that represents digits of a hexadecimal number using the characters 0-9 and either A-F or a-f.


collapse all

Convert a character vector that represents a hexadecimal value to a decimal number.

hexStr = '3FF';
D = hex2dec(hexStr)
D = 1023

Starting in R2019b, you can write values in hexadecimal format directly without using hex2dec. Use the 0x prefix and do not use quotation marks. MATLAB® stores the value as an integer, not as text.

D = 0x3FF
D = uint16

Create a string array that represents multiple hexadecimal values.

hexStr = ["3FF" "7A" "E"]
hexStr = 1x3 string array
    "3FF"    "7A"    "E"

Convert the hexadecimal values and return a numeric array.

D = hex2dec(hexStr)
D = 1×3

        1023         122          14

Starting in R2019b, it is recommended that you create a numeric array using hexadecimal literals instead of converting text with hex2dec.

D = [0x3FF 0x7A 0xE]
D = 1x3 uint16 row vector

   1023    122     14

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 character array with multiple rows or a cell array of character vectors, then the output is a numeric column vector.

  • If hexStr is a string array, then the output is a numeric array that has the same dimensions.

hexStr cannot represent a negative number.


  • If the input argument hexStr represents a value greater than the value returned by flintmax, then hex2dec might not return an exact conversion.

Extended Capabilities

Introduced before R2006a