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

0 Members and 1 Guest are viewing this topic.

Offline The Librarian

  • Moderator
  • Newbie
  • Posts: 42
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=2233.msg115086#msg115086
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 "Quick BIN$ test" 'b+ 2020-03-02  QB64 v1.4 < important for testing &B numbers
  2.  
  3.     INPUT "Enter a number to convert to test BIN$ > ", test&&
  4.     PRINT BIN$(test&&)
  5. LOOP UNTIL test&& = 0
  6.  
  7.  
  8. 'Function:  Convert any given dec/hex/oct number into a binary string.
  9. '           Can handle positive and negative values and works in that
  10. '           similar to the QB64 built-in HEX$ and OCT$ functions.
  11. '
  12. 'Synopsis:  binary$ = BIN$ (value&&)
  13. '
  14. 'Result:    binary$ --> The binary representation string of the given
  15. '                       number without leading zeros for positive values
  16. '                       and either 8/16/32 or 64 chars for negatives,
  17. '                       depending on the input size.
  18. '
  19. 'Inputs:    value&& --> The pos./neg. number to convert, may also be
  20. '                       given as &H or &O prefixed value.
  21. '
  22. 'Notes:     You may also pass in floating point values, as long as its
  23. '           represented value fits into the _INTEGER64 (&&) input, hence
  24. '           approx. -9.223372036854776E+18 to 9.223372036854776E+18.
  25. '           Different from HEX$ and OCT$, BIN$ won't throw an overflow
  26. '           error, if this range is exceeded, but the result is probably
  27. '           wrong in such a case.
  28. '---------------------------------------------------------------------
  29. FUNCTION BIN$ (value&&)
  30.     '--- option _explicit requirements ---
  31.     DIM temp~&&, charPos%, highPos%
  32.     '--- init ---
  33.     temp~&& = value&&
  34.     BIN$ = STRING$(64, "0"): charPos% = 64: highPos% = 64
  35.     '--- convert ---
  36.     DO
  37.         IF (temp~&& AND 1) THEN MID$(BIN$, charPos%, 1) = "1": highPos% = charPos%
  38.         charPos% = charPos% - 1: temp~&& = temp~&& \ 2
  39.     LOOP UNTIL temp~&& = 0
  40.     '--- adjust negative size ---
  41.     IF value&& < 0 THEN
  42.         IF -value&& < &H0080000000~&& THEN highPos% = 33
  43.         IF -value&& < &H0000008000~&& THEN highPos% = 49
  44.         IF -value&& < &H0000000080~&& THEN highPos% = 57
  45.     END IF
  46.     '--- set result ---
  47.     BIN$ = MID$(BIN$, highPos%)
  48.  


bplus edit note: RhoSigma is author of the &B updates to QB64 v1.4 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: March 07, 2020, 01:19:43 AM by The Librarian »