Main Content

dec2base

Convert decimal integer to its base-n representation

Description

example

baseStr = dec2base(D,n) returns a base-n representation of the decimal integer D. The output argument baseStr is a character array that represents digits using numeric characters and, when n is greater than 10, letters. For example, if n is 12, then dec2base represents the numbers 9, 10, and 11 using the characters 9, A, and B, and represents the number 12 as the character sequence 10.

If D is a numeric vector, matrix, or multidimensional array, then baseStr is a two-dimensional character array. Each row of baseStr represents an element of D.

example

baseStr = dec2base(D,n,minDigits) returns a base-n representation of D with no fewer than minDigits digits.

Examples

collapse all

Convert a decimal number to a character vector that represents its value in base 12. In this base system, the characters 'A' and 'B' represent the numbers denoted as 10 and 11 in base 10.

D = 23;
baseStr = dec2base(D,12)
baseStr = 
'1B'

Specify the number of base-12 digits that dec2base returns. If you specify more digits than are required, then dec2base pads the output with leading zeros.

D = 23;
baseStr = dec2base(D,12,6)
baseStr = 
'00001B'

If you specify fewer digits, then dec2base still returns as many digits as required to represent the input number.

baseStr = dec2base(D,12,1)
baseStr = 
'1B'

Create a numeric array.

D = [1023 122 14];

To represent the elements of D as octal, or base-8, values, use the dec2base function. Each row of baseStr corresponds to an element of D.

baseStr = dec2base(D,8)
baseStr = 3x4 char array
    '1777'
    '0172'
    '0016'

The dec2base function returns a character array padded with leading zeros. Starting in R2016b, the compose function is recommended for converting numeric arrays to octal representations. It returns a string array whose elements do not have leading zeros. To represent the elements of D as octal values, use the %o formatting operator.

hexStr = compose("%o",D)
hexStr = 1x3 string
    "1777"    "172"    "16"

Input Arguments

collapse all

Input array, specified as an array of nonnegative numbers. Each element of D must have a value between zero and the value returned by flintmax.

  • If D is an array of floating-point numbers, and any element of D has a fractional part, then dec2base produces an error. For example, dec2base(10,8) converts 10 to '12', but dec2base(10.5,8) produces an error.

  • If D is a character or logical array, then dec2base treats the elements of D as integers. However, dec2base treats characters as their Unicode® values, so specifying D as a character array is not recommended.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char

Base of output representation, specified as an integer between 2 and 36. For example, if n is 8, then the output represents base-8 numbers.

Minimum number of digits in output, specified as an integer.

  • If D can be represented with fewer than minDigits digits, then dec2base pads the output with leading zeros.

  • If D is so large that it must be represented with more than minDigits digits, then dec2base returns the output with as many digits as required.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced before R2006a