Main Content

bin2dec

Convert text representation of binary integer to double value

Description

example

D= bin2dec(binStr)converts the binary integer represented bybinStrto the equivalent decimal number and returns it as a double-precision floating-point value.

IfbinStrrepresents an integer greater than or equal toflintmax, thenbin2decmight not represent it exactly as a floating-point value.

Examples

collapse all

Convert a character vector that represents a binary value to a decimal number.

binStr ='10111'; D = bin2dec(binStr)
D = 23

从R2019b开始,你可以写在二进制值format directly without usingbin2dec. Use the0bprefix and do not use quotation marks. MATLAB® stores the value as an integer, not as text.

D = 0b10111
D =uint823

Create a string array that represents multiple binary values.

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

Convert the binary values and return a numeric array.

D = bin2dec(binStr)
D =1×31023 122 14

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

D = [0b1111111111 0b1111010 0b1110]
D =1x3 uint16 row vector1023 122 14

Input Arguments

collapse all

Text representing binary numbers, specified as a character array, cell array of character vectors, or string array.binStrrepresents binary digits using the characters0and1.

  • IfbinStris a character array with multiple rows or a cell array of character vectors, then the output is a numeric column vector.

  • IfbinStris a string array, then the output is a numeric array that has the same dimensions.

A binary number represented bybinStrcan have no more than 53 digits.

Starting in R2020a,binStrcan be text that includes the same prefixes (0bor0B) and suffixes used by binary literals. For example, these calls tobin2deceach return the number7.

bin2dec('111') bin2dec('0b111') bin2dec('0b111s32')

Also, in R2020abinStrcan 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 thes8suffix specifies the 8-bit signed integer type. In previous releases,binStrcannot represent a negative number.

Extended Capabilities

Version History

Introduced before R2006a

expand all

Behavior changed in R2020b