Convert decimal integer to its base-
baseStr = dec2base(
returns a base-
n representation of the decimal
D. The output argument
baseStr is a character
array that represents digits using numeric characters and, when
greater than 10, letters. For example, if
n is 12, then
dec2base represents the numbers 9, 10, and 11 using the characters
B, and represents the
number 12 as the character sequence
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
Convert Decimal Number
Convert a decimal number to a character vector that represents its value in base 12. In this base system, the characters
'B' represent the numbers denoted as 10 and 11 in base 10.
D = 23; baseStr = dec2base(D,12)
baseStr = '1B'
Specify Number of Digits
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'
Convert Numeric Array to Octal Values
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
baseStr = dec2base(D,8)
baseStr = 3x4 char array '1777' '0172' '0016'
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"
D — Input array
array of nonnegative numbers
Input array, specified as an array of nonnegative numbers. Each element of
D must have a value between zero and the value returned by
Dis an array of floating-point numbers, and any element of
Dhas a fractional part, then
dec2baseproduces an error. For example,
dec2base(10.5,8)produces an error.
Dis a character or logical array, then
dec2basetreats the elements of
Das integers. However,
dec2basetreats characters as their Unicode® values, so specifying
Das a character array is not recommended.
n — Base of output representation
integer between 2 and 36
Base of output representation, specified as an integer between 2 and 36. For
n is 8, then the output represents base-8 numbers.
minDigits — Minimum number of digits in output
Minimum number of digits in output, specified as an integer.
Dcan be represented with fewer than
dec2basepads the output with leading zeros.
Dis so large that it must be represented with more than
dec2basereturns the output with as many digits as required.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
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.