bin2num

Convert two's complement binary string to number using quantizer object

Description

example

y = bin2num(q,b) converts the binary character vector b to a numeric array y using the properties of the quantizer object q.

If b is a cell array containing binary strings, then y will be a cell array of the same dimension containing numeric arrays.

[y1,y2,…] = bin2num(q,b1,b2,…) converts the binary character vectors b1, b2, … to numeric arrays y1, y2, ….

Examples

collapse all

Convert between a binary character vector and a numeric array using the properties specified in a quantizer object.

Convert Numeric Array to Binary String

Create a quantizer object specifying a word length of 4 bits and a fraction length of 3 bits. The other properties of the quantizer object take the default values of specifying a signed, fixed-point data type, rounding towards negative infinity, and saturate on overflow.

q = quantizer([4 3])
q =

DataMode = fixed
RoundMode = floor
OverflowMode = saturate
Format = [4  3]

Create an array of numeric values.

[a,b] = range(q);
x = (b:-eps(q):a)
x = 1×16

0.8750    0.7500    0.6250    0.5000    0.3750    0.2500    0.1250         0   -0.1250   -0.2500   -0.3750   -0.5000   -0.6250   -0.7500   -0.8750   -1.0000

Convert the numeric vector x to binary representation using the properties specified by the quantizer object q. Note that num2bin always returns the binary representations in a column.

b = num2bin(q,x)
b = 16x4 char array
'0111'
'0110'
'0101'
'0100'
'0011'
'0010'
'0001'
'0000'
'1111'
'1110'
'1101'
'1100'
'1011'
'1010'
'1001'
'1000'

Use bin2num to perform the inverse operation.

y = bin2num(q,b)
y = 16×1

0.8750
0.7500
0.6250
0.5000
0.3750
0.2500
0.1250
0
-0.1250
-0.2500
⋮

Convert Binary String to Numeric Array

All of the 3-bit fixed-point two's-complement numbers in fractional form are given by:

q = quantizer([3 2]);
b = ['011  111'
'010  110'
'001  101'
'000  100'];

Use bin2num to view the numeric equivalents of these values.

x = bin2num(q,b)
x = 4×2

0.7500   -0.2500
0.5000   -0.5000
0.2500   -0.7500
0   -1.0000

Input Arguments

collapse all

Data type properties to use for conversion, specified as a quantizer object.

Example: q = quantizer([16 15]);

Binary string to convert, specified as a character vector, character array, or cell array containing binary strings.

Data Types: string | char | cell

Tips

• bin2num and num2bin are inverses of one another. Note that num2bin always returns the binary representations in a column.

Algorithms

• The fixed-point binary representation is two's complement.

• The floating-point binary representation is in IEEE® Standard 754 style.

• If there are fewer binary digits than are necessary to represent the number, then fixed-point zero-pads on the left, and floating-point zero-pads on the right. 