HEX$: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The HEX$ function returns the base 16 hexadecimal representation of an INTEGER, LONG or _INTEGER64 value as a STRING. {{PageSyntax}} :{{Parameter|result$}} = HEX$({{Parameter|decimalNumber}}) {{PageDescription}} * The function returns the string hexadecimal (base-16) representation of {{Parameter|decimalNumber}}. * The function does not return a leading sign space so LTRIM$ is not necessary. <!-- Confusing hack hidden: * Can be used in pla...")
 
No edit summary
Line 1: Line 1:
The [[HEX$]] function returns the base 16 hexadecimal representation of an [[INTEGER]], [[LONG]] or [[_INTEGER64]] value as a [[STRING]].
This function returns the hexadecimal (base 16) representation of any numeric value.




{{PageSyntax}}
{{PageSyntax}}
:{{Parameter|result$}} = [[HEX$]]({{Parameter|decimalNumber}})
: {{Parameter|hexvalue$}} = [[HEX$]]({{Parameter|number}})




{{PageDescription}}
{{PageDescription}}
* The function returns the string hexadecimal (base-16) representation of {{Parameter|decimalNumber}}.
* The function returns the base 16 (hexadecimal) representation of the given {{Parameter|number}} as [[STRING]].
* The function does not return a leading sign space so [[LTRIM$]] is not necessary.
** {{Parameter|number}} can be any [[INTEGER]], [[LONG]] or [[_INTEGER64]] value, positive or negative.
<!-- Confusing hack hidden: * Can be used in place of [[STR$]] to trim both sides of positive decimal values 0 to 9 only.}} -->
** {{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 will convert the ''-123'' only.
* [[VAL]] can convert the string value back to a decimal value by prefixing the string return with "&H": {{InlineCode}}dec = VAL("&H" + hexvar$){{InlineCodeEnd}}.
* 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 hex string value back to a decimal value by prefixing the string with "[[&H]]": {{InlineCode}}decimal = VAL("&H" + hexvalue$){{InlineCodeEnd}}.




{{PageExamples}}
{{PageExamples}}
''Example 1:'' Comparing decimal, hexadecimal and octal string values 0 to 15.
'''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


FOR n% = 0 TO 15
{{Cl|LOCATE}} 2, 10: {{Cl|PRINT}} tabletop$
  LOCATE 4 + n%, 20: {{Cl|PRINT USING}} template$; {{Cl|STR$}}(n%); {{Cl|HEX$}}(n%); {{Cl|OCT$}}(n%)
{{Cl|LOCATE}} 3, 10: {{Cl|PRINT}} tablesep$
NEXT n% '' ''
{{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}}
{{CodeEnd}}
:'''Note:''' Decimal [[STR$]] values contain a leading sign placeholder space so values require an extra space in the ''tableout$'' template using the slash format.
{{OutputStart}}
{{OutputStart}}
                    Decimal | Hexadecimal | Octal
 
                  -----------+-------------+--------
          Decimal | Hexadecimal | Octal | Binary
                      0    |     0       |   0   
        ---------+-------------+-------+--------
                      1    |     1       |   1
            0    |     0     |   |  0
                      2    |     2       |   2
            1    |     1      |  1   | 1
                      3    |     3       |   3
            2    |     2     |   2   |  10
                      4    |     4       |   4
            3    |     3     |   3   |  11
                      5    |     5       |   5
            4    |     4     |   4   |  100
                      6    |     6       |   6
            5    |     5     |   5   |  101
                      7    |     7       |   7
            6    |     6     |   6   |  110
                      8    |     8       |   10
            7    |     7     |   7   |  111
                      9    |     9       |   11
            8    |     8     |   10 |  1000
                      10    |     A       |   12
            9    |     9     |   11 |  1001
                      11    |     B       |   13
            10    |     A     |   12 |  1010
                      12    |     C       |   14
            11    |     B     |   13 |  1011
                      13    |     D       |   15
            12    |     C     |   14 |  1100
                      14    |     E       |   16
            13    |     D     |   15 |  1101
                      15    |     F       |   17
            14    |     E     |   16 |  1110
            15    |     F     |   17 |  1111
{{OutputEnd}}
{{OutputEnd}}
''Note:'' Decimal [[STR$]] values contain a leading sign space so values require an extra space in the template using the slash format.




''Example 2:'' Converting hex value to decimal.
'''Example 2:''' ''Converting a hexadecimal value to decimal.''
{{CodeStart}}
{{CodeStart}}
h$ = {{Cl|HEX$}}(255)
hexvalue$ = {{Cl|HEX$}}(255)
{{Cl|PRINT}} "Hex: "; h$
{{Cl|PRINT}} "Hex: "; hexvalue$
{{Cl|PRINT}} "Converting Hex value to Decimal:"; {{Cl|VAL}}("&H" + h$)
{{Cl|PRINT}} "Converting Hex value to Decimal:"; {{Cl|VAL}}("&H" + hexvalue$)
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Hex: FF
Hex: FF
Line 61: Line 64:


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




{{PageNavigation}}
{{PageNavigation}}

Revision as of 19:38, 28 April 2022

This function returns the hexadecimal (base 16) representation of any numeric value.


Syntax

hexvalue$ = HEX$(number)


Description

  • The function returns the base 16 (hexadecimal) representation of the given number as STRING.
    • 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 will convert the -123 only.
  • 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 hex string value back to a decimal value by prefixing the string with "&H": decimal = VAL("&H" + hexvalue$).


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: Decimal STR$ values contain a leading sign placeholder space so values require an extra space in the tableout$ template using the slash format.

          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 hexadecimal value to decimal.

hexvalue$ = HEX$(255)
PRINT "Hex: "; hexvalue$
PRINT "Converting Hex value to Decimal:"; VAL("&H" + hexvalue$)
Hex: FF
Converting Hex value to Decimal: 255


See also



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