D = bin2dec(binStr)
converts binStr to a decimal number and returns it. The input argument
binStr represents digits of a binary number using the characters
'0' and '1'.
Convert a character vector that represents a binary value to a decimal number.
binStr = '10111';
D = bin2dec(binStr)
D = 23
Starting in R2019b, you can write values in binary format directly without using bin2dec. Use the 0b prefix and do not use quotation marks. MATLAB® stores the value as an integer, not as text.
binStr — Text representing binary numbers character array | cell array of character vectors | string array
Text representing binary numbers, specified as a character array, cell array of
character vectors, or string array.
If binStr is a character array with multiple rows or a cell
array of character vectors, then the output is a numeric column vector.
If binStr is a string array, then the output is a numeric
array that has the same dimensions.
A binary number represented by binStr can have no more than 53
digits.
Starting in R2020a, binStr can be text that includes the same
prefixes (0b or 0B) and suffixes used by binary
literals. For example, these calls to bin2dec each return the
number 7.
Also, in R2020a binStr can represent the two's complement of a
negative number, using a suffix that specifies a signed integer type. For example,
bin2dec('0b11111111s8') returns -1, because the
s8 suffix specifies the 8-bit signed integer type. In previous
releases, binStr cannot represent a negative number.
Extended Capabilities
C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Input text must be specified as a character array or a string scalar. Cell arrays
are not supported.
When the input is empty, the answer does not match the answer in MATLAB®.
GPU Code Generation Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
Input text must be specified as a character array. Cell arrays are not
supported.
When the input is empty, the answer does not match the answer in MATLAB.