hex2dec
Convert text representation of hexadecimal integer to double value
Syntax
Description
converts the hexadecimal integer represented by D
= hex2dec(hexStr
)hexStr
to the equivalent
decimal number and returns it as a double-precision floating-point value.
If hexStr
represents an integer greater than or equal to
flintmax
, then hex2dec
might not represent it
exactly as a floating-point value.
Examples
Convert Text Representing Hexadecimal Value
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
1023
Return Numeric Array
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×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
hexStr
— Text representing hexadecimal numbers
character array | cell array of character vectors | string array
Text representing hexadecimal numbers, specified as a character array, cell array of
character vectors, or string array. hexStr
represents hexadecimal
digits using the characters 0
-9
and either
A
-F
or
a
-f
.
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.
Starting in R2020a, hexStr
can be text that includes the same
prefixes (0x
or 0X
) and suffixes used by
hexadecimal literals. For example, these calls to hex2dec
each
return the number 255
.
hex2dec('FF') hex2dec('0xFF') hex2dec('0xFFs32')
Also, in R2020a hexStr
can 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 the
s8
suffix specifies the 8-bit signed integer type. In previous
releases, hexStr
cannot represent a negative number.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Input text must be specified as a character array or string scalar. Cell arrays are not supported.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced before R2006aR2020b: Issue warning when input values are greater than or equal to flintmax
The hex2dec
function issues a warning when input values are greater than or equal to flintmax
. In previous releases, hex2dec
did not issue this warning.
hex2dec
now issues a warning because inputs representing
integers greater than or equal to flintmax
might not be represented
exactly as double-precision floating-point values.
To convert values greater than flintmax
exactly, consider one of
these alternatives:
In place of scalar text inputs, use hexadecimal or binary literals representing the same values. When you write a value as a literal, MATLAB® stores it as an integer that represents the value exactly. For more information, see Hexadecimal and Binary Values.
To convert hexadecimal inputs greater than
flintmax
, you can use thesscanf
function with the%lx
operator. When you use%lx
, the converted values are integers that have theuint64
data type. These integers have enough storage to represent values greater thanflintmax
exactly..
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)