Convert decimal integer to its hexadecimal representation
hexStr = dec2hex( returns the hexadecimal,
or base-16, representation of the decimal integer
D. The output argument
hexStr is a character vector that represents hexadecimal digits using
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
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
hexStr = dec2hex(D)
hexStr = 3x3 char array '3FF' '07A' '00E'
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 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.
ans = 'FF'
ans = 'F0'
D — Input array
Input array, specified as a numeric array.
Dis an array of floating-point numbers, and any element of
Dhas a fractional part, then
dec2hexproduces an error. For example,
'A', but does not convert
Dis a character or logical array, then
dec2hextreats the elements of
Das integers. However,
dec2hextreats characters as their Unicode® values, so specifying
Das a character array is not recommended.
D can include negative numbers. The function converts negative
numbers using their two's complement binary values.
minDigits — Minimum number of digits in output
Minimum number of digits in the output, specified as an integer.
Dcan be represented with fewer than
minDigitshexadecimal digits, then
dec2hexpads the output.
D >= 0
Pads with leading zeroes
D < 0
Pads with leading
Fcharacters (since R2020b)
Dis so large that it must be represented with more than
dec2hexreturns the output with as many digits as required.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
minDigitsis specified, the output will have that number of columns even if
Dis empty. If
minDigitsis not specified, the output will have at least one column.
single, then it must be greater than or equal to
intmin('int64')and less than
This function usually produces a variable-size output. To make the output fixed-size, supply
minDigitsas a constant large enough that the output has a fixed number of columns regardless of input values. For fixed-size output,
minDigitsmust be at least
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.