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 a decimal number to a character vector that represents its hexadecimal value.
D = 1023; hexStr = dec2hex(D)
hexStr = '3FF'
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'
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"
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.
D is an array of floating-point numbers, and any element
D has a fractional part, then
produces an error. For example,
'A', but does not convert
D is a character or logical array, then
dec2hex treats the elements of
dec2hex treats characters as their
Unicode® values, so specifying
D as a character array is not
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.
D can be represented with fewer than
minDigits hexadecimal digits, then
pads the output.
Pads with leading zeroes
Pads with leading
D is so large that it must be represented with more than
minDigits digits, then
dec2hex returns the
output with as many digits as required.
Usage notes and limitations:
minDigits is specified, the output will have that number of
columns even if
D is empty. If
minDigits is 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
minDigits as a constant large enough that the
output has a fixed number of columns regardless of input values. For fixed-size
minDigits must be at least