Convert text representation of binary number to decimal number



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'.


collapse all

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.

D = 0b10111
D = uint8

Create a string array that represents multiple binary values.

binStr = ["1111111111" "1111010" "1110"]
binStr = 1x3 string array
    "1111111111"    "1111010"    "1110"

Convert the binary values and return a numeric array.

D = bin2dec(binStr)
D = 1×3

        1023         122          14

Starting in R2019b, it is recommended that you create a numeric array using binary literals instead of converting text with bin2dec.

D = [0b1111111111 0b1111010 0b1110]
D = 1x3 uint16 row vector

   1023    122     14

Input Arguments

collapse all

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. Also, binStr cannot represent a negative number.

Extended Capabilities

Introduced before R2006a