BIN$ function

Purpose:

BIN$ converts an integer to a binary string.

Syntax:

RetStr = BIN$(Number AS INTEGER)

Return Value:

  • Data type: STRING
    RetStr string containing binary representation of the Number parameter argument.

Parameters:

  • Data type: INTEGER
    Number integer to be converted to binary.

Example 1:

DIM RetStr$

RetStr$ = BIN$(12345)

PRINT RetStr$

Result:

11000000111001

Example 2:

In BCX, a literal binary numeric value can be assigned to a variable.

$BCXVERSION "7.4.7 (2020/06/29)"

DIM Number

Number = 0b00000001001101000011110010110100

PRINT Number

Result:

20200628

Example 3:

SET BinCode[] AS INTEGER
 0b0000,
 0b0001,
 0b0010,
 0b0011,
 0b0100,
 0b0101,
 0b0110,
 0b0111,
 0b1000,
 0b1001,
 0b1010,
 0b1011,
 0b1100,
 0b1101,
 0b1110,
 0b1111
END SET


PRINT "Binary ", "Gray Code"
FOR INTEGER i = 0 TO 15
 PRINT " ", LPAD$(BIN$(BinCode[i]), 4, ASC("0")), "   ";
 PRINT LPAD$(BIN$((BinCode[i] XOR (BinCode[i] >> 1))), 4, ASC("0"))
NEXT i

Result:

Binary Gray Code
0000   0000
0001   0001
0010   0011
0011   0010
0100   0110
0101   0111
0110   0101
0111   0100
1000   1100
1001   1101
1010   1111
1011   1110
1100   1010
1101   1011
1110   1001
1111   1000

BIN2DEC function

Purpose:

BIN2DEC converts a binary string to an integer.

Syntax:

RetVal = BIN2DEC(BinaryMainStr AS STRING)

Parameters:

  • Data type: INTEGER
    RetVal Returned integer containing decimal representation of BinaryMainStr$ parameter.
  • Data type: STRING
    BinaryMainStr String containing binary number to be converted to decimal integer.

Example:

DIM RetVal%
RetVal% = BIN2DEC("0101")
PRINT RetVal%

Result:

5