Main Content

hex2dec

Convert text representation of hexadecimal integer to double value

Description

example

D= hex2dec(hexStr)converts the hexadecimal integer represented byhexStrto the equivalent decimal number and returns it as a double-precision floating-point value.

IfhexStrrepresents an integer greater than or equal toflintmax, thenhex2decmight not represent it exactly as a floating-point value.

Examples

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 usinghex2dec. Use the0xprefix and do not use quotation marks. MATLAB® stores the value as an integer, not as text.

D = 0x3FF
D =uint161023

Create a string array that represents multiple hexadecimal values.

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

Convert the hexadecimal values and return a numeric array.

D = hex2dec(hexStr)
D =1×31023 122 14

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

D = [0x3FF 0x7A 0xE]
D =1x3 uint16 row vector1023 122 14

Input Arguments

collapse all

Text representing hexadecimal numbers, specified as a character array, cell array of character vectors, or string array.hexStrrepresents hexadecimal digits using the characters0-9and eitherA-Fora-f.

  • IfhexStris a character array with multiple rows or a cell array of character vectors, then the output is a numeric column vector.

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

Starting in R2020a,hexStrcan be text that includes the same prefixes (0xor0X) and suffixes used by hexadecimal literals. For example, these calls tohex2deceach return the number255.

hex2dec('FF') hex2dec('0xFF') hex2dec('0xFFs32')

Also, in R2020ahexStrcan represent the two's complement of a negative number, using a suffix that specifies a signed integer type. For example,hex2dec('0xFFs8')returns-1, because thes8suffix specifies the 8-bit signed integer type. In previous releases,hexStrcannot represent a negative number.

Extended Capabilities

Version History

Introduced before R2006a

expand all

Behavior changed in R2020b