dec2hex
Convert decimal integer to its hexadecimal representation
Description
hexStr = dec2hex(
returns the hexadecimal,
or base-16, representation of the decimal integer D
)D
. The output argument
hexStr
is a character vector that represents hexadecimal digits using
the characters 0
-9
and
A
-F
.
If D
is a numeric vector, matrix, or multidimensional array, then
hexStr
is a two-dimensional character array. Each row of
hexStr
represents an element of D
.
Examples
Convert Decimal Number
Convert a decimal number to a character vector that represents its hexadecimal value.
D = 1023; hexStr = dec2hex(D)
hexStr = '3FF'
Specify Minimum Number of Digits
Specify the minimum number of hexadecimal digits that dec2hex
returns. If you specify more digits than are required, then dec2hex
pads the output.
D = 1023; hexStr = dec2hex(D,6)
hexStr = '0003FF'
If you specify fewer digits, then dec2hex
still returns as many hexadecimal digits as required to represent the input number.
hexStr = dec2hex(D,1)
hexStr = '3FF'
Convert Numeric Array
Create a numeric array.
D = [1023 122 14];
To represent the elements of D
as hexadecimal values, use the dec2hex
function. Each row of hexStr
corresponds to an element of D
.
hexStr = dec2hex(D)
hexStr = 3x3 char array
'3FF'
'07A'
'00E'
The dec2hex
function returns a padded character array. Starting in R2016b, the compose
function is recommended for converting numeric arrays to hexadecimal representations. It returns a string array whose elements are not padded. To represent the elements of D
as hexadecimal values, use either the %X
or %x
formatting operator.
hexStr = compose("%X",D)
hexStr = 1x3 string
"3FF" "7A" "E"
Represent Negative Numbers
Starting in R2020a, the dec2hex
function converts negative numbers using their two's complement binary values.
For example, these calls to dec2hex
convert negative numbers.
dec2hex(-1)
ans = 'FF'
dec2hex(-16)
ans = 'F0'
Input Arguments
D
— Input array
numeric array | char
array | logical array
Input array, specified as a numeric array, char
array, or logical
array.
If
D
is an array of floating-point numbers, and any element ofD
has a fractional part, thendec2hex
produces an error. For example,dec2hex
converts10
to'A'
but does not convert10.5
.If
D
is a character or logical array, thendec2hex
treats the elements ofD
as integers. However,dec2hex
treats characters as their Unicode® values, so specifyingD
as a character array is not recommended.
Since R2020a
D
can include negative numbers. The function converts negative
numbers using their two's complement binary values.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
minDigits
— Minimum number of digits in output
nonnegative integer
Minimum number of digits in the output, specified as a nonnegative integer.
If
D
can be represented with fewer thanminDigits
hexadecimal digits, thendec2hex
pads the output.D >= 0
Pads with leading zeros
D < 0
Pads with leading
F
characters (since R2020b)If
D
is so large that it must be represented with more thanminDigits
digits, thendec2hex
returns the output with as many digits as required.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
If
minDigits
is specified, the output will have that number of columns even ifD
is empty. IfminDigits
is not specified, the output will have at least one column.If input
D
isdouble
orsingle
, then it must be greater than or equal tointmin('int64')
and less than2^64
.This function usually produces a variable-size output. To make the output fixed-size, supply
minDigits
as a constant large enough that the output has a fixed number of columns regardless of input values. For fixed-size output,minDigits
must be at least16
fordouble
,16
forsingle
,8
forhalf
,1
forlogical
,2
forchar
,16
forint64
,16
foruint64
,8
forint32
,8
foruint32
,4
forint16
,4
foruint16
,2
forint8
, and2
foruint8
.
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 R2006aR2022a: Restrict input type to primitive numeric types
User-defined datatypes are restricted to primitive numeric types and classes that inherit from a primitive numeric type.
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)