bin2dec
Convert text representation of binary integer to double value
Syntax
Description
converts the binary integer represented by D
= bin2dec(binStr
)binStr
to the equivalent
decimal number and returns it as a double-precision floating-point value.
If binStr
represents an integer greater than or equal to
flintmax
, then bin2dec
might not represent it
exactly as a floating-point value.
Examples
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
23
Create a string array that represents multiple binary values.
binStr = ["1111111111" "1111010" "1110"]
binStr = 1×3 string
"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 = 1×3 uint16 row vector
1023 122 14
Input Arguments
Text representing binary numbers, specified as a character array, cell array of
character vectors, or string array. binStr
represents binary digits
using the characters 0
and 1
.
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
.
bin2dec('111') bin2dec('0b111') bin2dec('0b111s32')
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
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®.
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.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced before R2006aThe bin2dec
function issues a warning when input values are greater than or equal to flintmax
. In previous releases, bin2dec
did not issue this warning.
bin2dec
now issues a warning because inputs representing
integers greater than or equal to flintmax
might not be represented
exactly as double-precision floating-point values.
To convert values greater than flintmax
exactly, consider one of
these alternatives:
In place of scalar text inputs, use hexadecimal or binary literals representing the same values. When you write a value as a literal, MATLAB stores it as an integer that represents the value exactly. For more information, see Hexadecimal and Binary Values.
To convert hexadecimal inputs greater than
flintmax
, you can use thesscanf
function with the%lx
operator. When you use%lx
, the converted values are integers that have theuint64
data type. These integers have enough storage to represent values greater thanflintmax
exactly..
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)