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.

example

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.

example

Examples

collapse all

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

collapse all

Input array, specified as a numeric array, char array, or logical array.

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.

Extended Capabilities

expand all

Usage notes and limitations:

Version History

Introduced before R2006a

expand all

User-defined datatypes are restricted to primitive numeric types and classes that inherit from a primitive numeric type.