OCT$: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The OCT$ function returns the base-8 octal representation of an INTEGER, LONG or _INTEGER64 value as a STRING. {{PageSyntax}} : {{Parameter|result$}} = OCT$({{Parameter|number}}) {{PageDescription}} * The OCT$ function returns the octal (base-8) representation of {{Parameter|number}}. * {{Parameter|number}} can be any integer value. * No leading space is returned. * VAL can convert octal string values to decimal when the "&O" prefix is...")
 
No edit summary
Line 1: Line 1:
The [[OCT$]] function returns the base-8 octal representation of an [[INTEGER]], [[LONG]] or [[_INTEGER64]] value as a [[STRING]].
This function returns the octal (base 8) representation of any numeric value.




{{PageSyntax}}
{{PageSyntax}}
: {{Parameter|result$}} = [[OCT$]]({{Parameter|number}})
: {{Parameter|octvalue$}} = [[OCT$]]({{Parameter|number}})
 
 
{{PageParameters}}
* {{Parameter|number}} can be any [[INTEGER]], [[LONG]] or [[_INTEGER64]] value, positive or negative.
* {{Parameter|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.




{{PageDescription}}
{{PageDescription}}
* The [[OCT$]] function returns the octal (base-8) representation of {{Parameter|number}}.
* The function returns the base 8 (octal) representation of the given {{Parameter|number}} as [[STRING]].
* {{Parameter|number}} can be any integer value.
* 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.
* No leading space is returned.
* [[VAL]] can convert the returned oct string value back to a decimal value by prefixing the string with "[[&O]]".
* [[VAL]] can convert octal string values to decimal when the "&O" prefix is added.
** Eg. {{InlineCode}}decimal = VAL("&O" + octvalue$){{InlineCodeEnd}}.




{{PageExamples}}
{{PageExamples}}
''Example:'' Outputs all of the decimal, hexadecimal and octal digits:
;Example 1: Comparing decimal, hexadecimal, octal and binary string values from 0 to 15.
{{CodeStart}}
{{CodeStart}}
LOCATE 2, 20: PRINT " Decimal | Hexadecimal | Octal "
tabletop$ = " Decimal | Hexadecimal | Octal | Binary "
LOCATE 3, 20: PRINT "---------+-------------+-------"
tablesep$ = "---------+-------------+-------+--------"
        template$ = "    ##   |     \\      |  ## "
tableout$ = " \ \   |      \\    |   \\  | \ \ " 'the PRINT USING template
 
{{Cl|LOCATE}} 2, 10: {{Cl|PRINT}} tabletop$
{{Cl|LOCATE}} 3, 10: {{Cl|PRINT}} tablesep$
{{Cl|FOR...NEXT|FOR}} n% = 0 {{Cl|TO}} 15
    {{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}}
;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.
{{OutputStart}}
          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
{{OutputEnd}}


FOR n% = 0 TO 15
  LOCATE 4 + n%, 20: {{Cl|PRINT USING}} template$; n%; {{Cl|HEX$}}(n%); VAL({{Cl|OCT$}}(n%))
NEXT n%


;Example 2:Converting a octal value to decimal.
{{CodeStart}}
octvalue$ = {{Cl|OCT$}}(255)
{{Cl|PRINT}} "Oct: "; octvalue$
{{Cl|PRINT}} "Converting Oct value to Decimal:"; {{Cl|VAL}}("&O" + octvalue$)
{{CodeEnd}}
{{CodeEnd}}
: ''Note:'' The actual octal value is converted by [[VAL]] directly back to a numerical value by '''not using''' the "&H" prefix.
{{OutputStart}}
{{OutputStart}}
                    Decimal | Hexadecimal | Octal
Oct: 377
                  ---------+-------------+-------
Converting Oct value to Decimal: 255
                        0  |    0      |    0 
                        1  |    1      |    1
                        2  |    2      |    2
                        3  |    3      |    3
                        4  |    4      |    4
                        5  |    5      |    5
                        6  |    6      |    6
                        7  |    7      |    7
                        8  |    8      |  10
                        9  |    9      |  11
                      10  |    A      |  12
                      11  |    B      |  13
                      12  |    C      |  14
                      13  |    D      |  15
                      14  |    E      |  16
                      15  |    F      |  17
{{OutputEnd}}
{{OutputEnd}}




{{PageSeeAlso}}
{{PageSeeAlso}}
* [[HEX$]], [[VAL]]
* [[_BIN$]], [[HEX$]], [[STR$]], [[VAL]]
* [[&H]] {{text|(hexadecimal)}}, [[&O]] {{text|(octal)}}, [[&B]] {{text|(binary)}}
* [[&B]] {{text|(binary)}}, [[&H]] {{text|(hexadecimal)}}, [[&O]] {{text|(octal)}}
* [[Base Comparisons]]
* [[Base Comparisons]]




{{PageNavigation}}
{{PageNavigation}}

Revision as of 22:23, 28 April 2022

This function returns the octal (base 8) representation of any numeric value.


Syntax

octvalue$ = OCT$(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 8 (octal) 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 oct string value back to a decimal value by prefixing the string with "&O".
    • Eg. decimal = VAL("&O" + octvalue$).


Examples

Example 1
Comparing decimal, hexadecimal, octal and binary string values from 0 to 15.
tabletop$ = " Decimal | Hexadecimal | Octal | Binary "
tablesep$ = "---------+-------------+-------+--------"
tableout$ = "  \ \    |      \\     |   \\  |  \  \  " 'the PRINT USING template

LOCATE 2, 10: PRINT tabletop$
LOCATE 3, 10: PRINT tablesep$
FOR n% = 0 TO 15
    LOCATE 4 + n%, 10: PRINT USING tableout$; STR$(n%); HEX$(n%); OCT$(n%); _BIN$(n%)
NEXT n%
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 octal value to decimal.
octvalue$ = OCT$(255)
PRINT "Oct: "; octvalue$
PRINT "Converting Oct value to Decimal:"; VAL("&O" + octvalue$)
Oct: 377
Converting Oct value to Decimal: 255


See also



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