Convert decimal number to character array representing binary number
binStr = dec2bin( returns the binary, or
base-2, representation of the number
D. The output argument
binStr is a character vector that represents binary digits using the
D is a numeric vector, matrix, or multidimensional array, then
binStr is a two-dimensional character array. Each row of
binStr represents an element of
Convert a decimal number to a character vector that represents its binary value.
D = 23; binStr = dec2bin(D)
binStr = '10111'
Specify the number of binary digits that
dec2bin returns. If you specify more digits are required, then
dec2bin pads the output with leading zeros.
D = 23; binStr = dec2bin(D,8)
binStr = '00010111'
If you specify fewer digits, then
dec2bin still returns as many binary digits as required to represent the input number.
binStr = dec2bin(D,1)
binStr = '10111'
Create a numeric array.
D = [1023 122 14];
To represent the elements of
D as binary values, use the
dec2bin function. Each row of
binStr corresponds to an element of
binStr = dec2bin(D)
binStr = 3x10 char array '1111111111' '0001111010' '0000001110'
Since all rows of a character array must have the same number of characters,
dec2bin pads some rows of
binStr with leading zeros. For example, the number
14 can be represented by the binary digits
'1110'. But to match the length of the first row of
dec2bin function pads the third row to
D— Input array
Input array, specified as an array of nonnegative numbers.
D is an array of floating-point numbers, and any element
D has a fractional part, then
truncates it before conversion. For example,
The truncation is always to the nearest integer less than or equal to that
D is a character or logical array, then
dec2bin treats the elements of
dec2bin treats characters as their
Unicode® values, so specifying
D as a character array is not
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 binary digits, then
the output with leading zeros.
D is so large that it must be represented with more than
minDigits digits, then
dec2bin returns the
output with as many digits as required.
If the input argument
D has a value greater than the value returned
dec2bin might not return an
The output of
dec2bin is the same whether your computer stores
values in memory using big-endian or little-endian format. For more information on these
formats, see Endianness.
Usage notes and limitations:
double, then it must be less
single, then it must be less
Unless you specify input
minDigits to be constant and
minDigits is large enough that the output has a fixed number of
columns regardless of the input values, this function requires variable-sizing
support. Without variable-sizing support,
minDigits must be at
int16, and so on.