BIN$: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
Tag: Manual revert
No edit summary
Line 16: Line 16:
* Different from [[STR$]], this function does not return a leading sign placeholder space, so no [[LTRIM$]] to strip that space from positive numbers is necessary.
* Different from [[STR$]], this function does not return a leading sign placeholder space, so no [[LTRIM$]] to strip that space from positive numbers is necessary.
* [[VAL]] can convert the returned bin string value back to a decimal value by prefixing the string with "[[&B]]".
* [[VAL]] can convert the returned bin string value back to a decimal value by prefixing the string with "[[&B]]".
** Eg. {{InlineCode}}decimal = {{Cl|VAL}}("&B" + binvalue$){{InlineCodeEnd}}.
** Eg. {{InlineCode}}decimal = {{Cl|VAL}}({{Text|<nowiki>"&B"</nowiki>|#FFB100}} + binvalue$){{InlineCodeEnd}}.




Line 27: Line 27:
;Example 1:Comparing decimal, hexadecimal, octal and binary string values from 0 to 15.
;Example 1:Comparing decimal, hexadecimal, octal and binary string values from 0 to 15.
{{CodeStart}}
{{CodeStart}}
tabletop$ = " Decimal | Hexadecimal | Octal | Binary "
{{Cl|SCREEN}} {{Cl|_NEWIMAGE}}({{Text|640|#F580B1}}, {{Text|480|#F580B1}}, {{Text|32|#F580B1}})
tablesep$ = "---------+-------------+-------+--------"
BG& = {{Cl|POINT}}({{Text|1|#F580B1}}, {{Text|1|#F580B1}})
tableout$ = "  \ \    |      \\    |  \\  |  \  \  " 'the PRINT USING template
{{Cl|PRINT}} {{Text|<nowiki>"Alpha ="</nowiki>|#FFB100}}; {{Cl|_ALPHA}}(BG&); {{Text|<nowiki>"Press a key to use CLS!"</nowiki>|#FFB100}}
 
K$ = {{Cl|INPUT$}}({{Text|1|#F580B1}})
{{Cl|LOCATE}} 2, 10: {{Cl|PRINT}} tabletop$
{{Cl|CLS}}
{{Cl|LOCATE}} 3, 10: {{Cl|PRINT}} tablesep$
BG& = {{Cl|POINT}}({{Text|1|#F580B1}}, {{Text|1|#F580B1}})
{{Cl|FOR...NEXT|FOR}} n% = 0 {{Cl|TO}} 15
{{Cl|PRINT}} {{Text|<nowiki>"CLS Alpha ="</nowiki>|#FFB100}}; {{Cl|_ALPHA}}(BG&)
    {{Cl|LOCATE}} 4 + n%, 10: {{Cl|PRINT USING}} tableout$; {{Cl|STR$}}(n%); {{Cl|HEX$}}(n%); {{Cl|OCT$}}(n%); {{Cl|_BIN$}}(n%)
{{Cl|NEXT}} n%
{{CodeEnd}}
{{CodeEnd}}
;Note:Although the decimal numbers 0-15 have a maximum width of 2 digits only, an extra space in the ''tableout$'' template is needed when using the (fixed width string) slash output format, as [[STR$]] values contain a leading sign placeholder space.
;Note:Although the decimal numbers 0-15 have a maximum width of 2 digits only, an extra space in the ''tableout$'' template is needed when using the (fixed width string) slash output format, as [[STR$]] values contain a leading sign placeholder space.
Line 62: Line 60:
;Example 2:Converting a binary value to decimal.
;Example 2:Converting a binary value to decimal.
{{CodeStart}}
{{CodeStart}}
binvalue$ = {{Cl|_BIN$}}(255)
binvalue$ = {{Cl|_BIN$}}({{Text|255|#F580B1}})
{{Cl|PRINT}} "Bin: "; binvalue$
{{Cl|PRINT}} {{Text|<nowiki>"Bin: "</nowiki>|#FFB100}}; binvalue$
{{Cl|PRINT}} "Converting Bin value to Decimal:"; {{Cl|VAL}}("&B" + binvalue$)
{{Cl|PRINT}} {{Text|<nowiki>"Converting Bin value to Decimal:"</nowiki>|#FFB100}}; {{Cl|VAL}}({{Text|<nowiki>"&B"</nowiki>|#FFB100}} + binvalue$)
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}

Revision as of 16:44, 19 March 2023

This function returns the binary (base 2) representation of any numeric value.


Syntax

binvalue$ = _BIN$(number)


Parameters

  • number can be any INTEGER, LONG or _INTEGER64 value, positive or negative.
  • number can also be any SINGLE, DOUBLE or _FLOAT value, but only the integer part of the value is converted in that case. That is, from the value -123.45 the function would convert the -123 only.


Description

  • The function returns the base 2 (binary) representation of the given number as STRING.
  • Different from STR$, this function does not return a leading sign placeholder space, so no LTRIM$ to strip that space from positive numbers is necessary.
  • VAL can convert the returned bin string value back to a decimal value by prefixing the string with "&B".
    • Eg. decimal = VAL("&B" + binvalue$).


Availability

  • QB64 v2.1 and up
  • QB64-PE all versions


Examples

Example 1
Comparing decimal, hexadecimal, octal and binary string values from 0 to 15.
SCREEN _NEWIMAGE(640, 480, 32)
BG& = POINT(1, 1)
PRINT "Alpha ="; _ALPHA(BG&); "Press a key to use CLS!"
K$ = INPUT$(1)
CLS
BG& = POINT(1, 1)
PRINT "CLS Alpha ="; _ALPHA(BG&)
Note
Although the decimal numbers 0-15 have a maximum width of 2 digits only, an extra space in the tableout$ template is needed when using the (fixed width string) slash output format, as STR$ values contain a leading sign placeholder space.
          Decimal | Hexadecimal | Octal | Binary
         ---------+-------------+-------+--------
            0     |      0      |   0   |  0
            1     |      1      |   1   |  1
            2     |      2      |   2   |  10
            3     |      3      |   3   |  11
            4     |      4      |   4   |  100
            5     |      5      |   5   |  101
            6     |      6      |   6   |  110
            7     |      7      |   7   |  111
            8     |      8      |   10  |  1000
            9     |      9      |   11  |  1001
            10    |      A      |   12  |  1010
            11    |      B      |   13  |  1011
            12    |      C      |   14  |  1100
            13    |      D      |   15  |  1101
            14    |      E      |   16  |  1110
            15    |      F      |   17  |  1111


Example 2
Converting a binary value to decimal.
binvalue$ = _BIN$(255)
PRINT "Bin: "; binvalue$
PRINT "Converting Bin value to Decimal:"; VAL("&B" + binvalue$)
Bin: 11111111
Converting Bin value to Decimal: 255


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link