Author Topic: BIN$ (binary converter) by RhoSigma  (Read 2174 times)

0 Members and 1 Guest are viewing this topic.

Offline The Librarian

  • Moderator
  • Newbie
  • Posts: 40
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.     PRINT
  12.      
  13.     PRINT "works also with &B, &H and &O..."
  14.     PRINT " &B1101 = "; Bin$(&B1101)
  15.     PRINT " &H211 = "; Bin$(&H211)
  16.     PRINT " &O377 = "; Bin$(&O377)
  17.     PRINT
  18.      
  19.     PRINT "and even with floating points (converts integer part only)..."
  20.     num# = 123.456: PRINT num#; "= "; Bin$(num#)
  21.     num# = -60000.25: PRINT num#; "= "; Bin$(num#)
  22.     num# = 0.5: PRINT num#; "= "; Bin$(num#)
  23.     PRINT
  24.      
  25.     '-- done
  26.     END
  27.      
  28.      
  29.      
  30.      
  31.     '--- Full description available in separate HTML document.
  32.     '---------------------------------------------------------------------
  33.     FUNCTION Bin$ (value&&)
  34.     '--- option _explicit requirements ---
  35.     DIM temp~&&, binStr$, charPos%, highPos%
  36.     '--- init ---
  37.     temp~&& = value&&
  38.     binStr$ = STRING$(64, "0"): charPos% = 64: highPos% = 64
  39.     '--- convert ---
  40.     DO
  41.         IF (temp~&& AND 1) THEN MID$(binStr$, charPos%, 1) = "1": highPos% = charPos%
  42.         charPos% = charPos% - 1: temp~&& = temp~&& \ 2
  43.     LOOP UNTIL temp~&& = 0
  44.     '--- adjust negative size ---
  45.     IF value&& < 0 THEN
  46.         IF -value&& < &H0080000000~&& THEN highPos% = 33
  47.         IF -value&& < &H0000008000~&& THEN highPos% = 49
  48.         IF -value&& < &H0000000080~&& THEN highPos% = 57
  49.     END IF
  50.     '--- set result ---
  51.     Bin$ = MID$(binStr$, highPos%)
  52.      
  53.  

 
 

 

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 »