dec2hex - Convert decimal integer to its hexadecimal representation - MATLAB (original) (raw)
Convert decimal integer to its hexadecimal representation
Syntax
Description
hexStr = dec2hex([D](#mw%5F649ffcc7-9285-427b-9fb9-d2e508e80cda))
returns the hexadecimal, or base-16, representation of the decimal integer D
. The output argumenthexStr
is a character vector that represents hexadecimal digits using the characters 0
-9
andA
-F
.
If D
is a numeric vector, matrix, or multidimensional array, thenhexStr
is a two-dimensional character array. Each row ofhexStr
represents an element of D
.
hexStr = dec2hex([D](#mw%5F649ffcc7-9285-427b-9fb9-d2e508e80cda),[minDigits](#mw%5F1d224aea-78ae-455e-b47f-1b8ac25b6e59))
returns a hexadecimal representation with no fewer than minDigits
digits.
Examples
Convert a decimal number to a character vector that represents its hexadecimal value.
D = 1023; hexStr = dec2hex(D)
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)
If you specify fewer digits, then dec2hex
still returns as many hexadecimal digits as required to represent the input number.
Create a numeric array.
To represent the elements of D
as hexadecimal values, use the dec2hex
function. Each row of hexStr
corresponds to an element of D
.
hexStr = 3×3 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 = 1×3 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.
Input Arguments
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
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
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
.
Version History
Introduced before R2006a
User-defined datatypes are restricted to primitive numeric types and classes that inherit from a primitive numeric type.