### Author Topic: BIN\$ (binary converter) by RhoSigma  (Read 4635 times)

0 Members and 1 Guest are viewing this topic.

#### The Librarian

• Moderator
• Newbie
• Posts: 39
##### BIN\$ (binary converter) by RhoSigma
« on: August 11, 2019, 01:50:39 pm »
BIN\$ (binary converter)

Contributor(s): @RhoSigma
Source: qb64.org Forum
URL: https://www.qb64.org/forum/index.php?topic=4261.msg136393#msg136393
Tags: [binary] [number conversion]

Description:
Although there are already many approaches for a BIN\$ function in the Forum, some of my own, some from bplus and Steve McNeill and probably others, I felt there should be one, which best mimics the regular behavior and results of the built-in HEX\$ and OCT\$ functions, rather than focusing on speed or extended flexibility.

So here it is:
can handle positive and negative numbers
returns the binary string without &B prefix, just as HEX\$ and OCT\$ do
the result for positive numbers is just as long as needed, ie. no leading zeros are returned
the result length for negative numbers is determined by the integer range, which the given input number does fit in, eg. 8 for numbers in _BYTE range (down until -128), 16 for INTEGER (down until -32768) etc..

Source Code:
Code: QB64: [Select]
1.     _TITLE "Bin\$ Example"
2.     '=== Full description for the Bin\$() function is available
3.     '=== in the separate HTML document.
4.     '=====================================================================
5.
6.     '-- some usage examples
7.     PRINT "some simple numbers..."
8.     num& = 5: PRINT num&; "= "; Bin\$(num&)
9.     num& = -4: PRINT num&; "= "; Bin\$(num&)
10.     num& = 32000: PRINT num&; "= "; Bin\$(num&)
11.
12.     PRINT "works also with &B, &H and &O..."
13.     PRINT " &B1101 = "; Bin\$(&B1101)
14.     PRINT " &H211 = "; Bin\$(&H211)
15.     PRINT " &O377 = "; Bin\$(&O377)
16.
17.     PRINT "and even with floating points (converts integer part only)..."
18.     num# = 123.456: PRINT num#; "= "; Bin\$(num#)
19.     num# = -60000.25: PRINT num#; "= "; Bin\$(num#)
20.     num# = 0.5: PRINT num#; "= "; Bin\$(num#)
21.
22.     '-- done
23.
24.
25.
26.
27.     '--- Full description available in separate HTML document.
28.     '---------------------------------------------------------------------
29.     FUNCTION Bin\$ (value&&)
30.     '--- option _explicit requirements ---
31.     DIM temp~&&, binStr\$, charPos%, highPos%
32.     '--- init ---
33.     temp~&& = value&&
34.     binStr\$ = STRING\$(64, "0"): charPos% = 64: highPos% = 64
35.     '--- convert ---
36.         IF (temp~&& AND 1) THEN MID\$(binStr\$, charPos%, 1) = "1": highPos% = charPos%
37.         charPos% = charPos% - 1: temp~&& = temp~&& \ 2
38.     LOOP UNTIL temp~&& = 0
39.     '--- adjust negative size ---
40.     IF value&& < 0 THEN
41.         IF -value&& < &H0080000000~&& THEN highPos% = 33
42.         IF -value&& < &H0000008000~&& THEN highPos% = 49
43.         IF -value&& < &H0000000080~&& THEN highPos% = 57
44.     '--- set result ---
45.     Bin\$ = MID\$(binStr\$, highPos%)
46.
47.

Librarian's note: RhoSigma is author of the &B updates to QB64, and this BIN\$ should serve as best complement to &B thus the replacement of the old BIN\$ code; screenshot contains new range of numbers that this BIN\$ can do.
« Last Edit: October 12, 2021, 10:31:59 am by Junior Librarian »