HEX$: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
No edit summary |
No edit summary |
||
Line 28: | Line 28: | ||
{{Cl|NEXT}} n% | {{Cl|NEXT}} n% | ||
{{CodeEnd}} | {{CodeEnd}} | ||
;Note: | ;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}} | {{OutputStart}} | ||
Decimal | Hexadecimal | Octal | Binary | Decimal | Hexadecimal | Octal | Binary |
Revision as of 21:31, 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.
- 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".
- Eg. 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
- 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 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
- _BIN$, OCT$, STR$, VAL
- &B (binary), &H (hexadecimal), &O (octal)
- Base Comparisons
- HEX$ 32 Bit Values