&H: Difference between revisions
Jump to navigation
Jump to search
Code by CodeGuy
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 55: | Line 55: | ||
h$ = {{Cl|HEX$}}(n&) 'get hexadecimal string value | h$ = {{Cl|HEX$}}(n&) 'get hexadecimal string value | ||
{{Cl|FOR}} i = 1 {{Cl|TO}} {{Cl|LEN}}(h$) 'scan the HEX$ digits | {{Cl|FOR}} i = 1 {{Cl|TO}} {{Cl|LEN}}(h$) 'scan the HEX$ digits | ||
{{Cl|SELECT CASE}} {{Cl|MID$}}(h$, i, 1) 'read each HEX$ digit | {{Cl|SELECT CASE}} {{Cl|MID$ (function)|MID$}}(h$, i, 1) 'read each HEX$ digit | ||
{{Cl|CASE}} "0": b$ = b$ + "0000" | {{Cl|CASE}} "0": b$ = b$ + "0000" | ||
{{Cl|CASE}} "1": b$ = b$ + "0001" | {{Cl|CASE}} "1": b$ = b$ + "0001" |
Revision as of 00:29, 26 February 2023
The &H prefix denotes that an integer value is expressed in a Hexadecimal base 16 format. Every 2 digits represent a _BYTE.
Syntax
- a& = &HC0DEBA5E
Description
- The base 16 numbering system uses hexadecimal digit values of 0 to F, where A = 10, B = 11, C = 12, D = 13, E = 14 and F = 15.
- Leading zero values can be omitted just like in decimal values as they add nothing to the return value.
- Decimal values returned can be any signed INTEGER, LONG integer, or _INTEGER64 value so use those type of variables when converting directly as shown above in the Syntax. The program "overflow" error limits are listed as:
- _BYTE: 2 hex digits or a decimal value range from -128 to 127. _UNSIGNED: 0 to 255.
- INTEGER: 4 hex digits or a decimal value range from -32,768 to 32,767. _UNSIGNED: 0 to 65535.
- LONG: 8 hex digits or a decimal value range from -2,147,483,648 to 2,147,483,647. _UNSIGNED: 0 to 4294967295.
- _INTEGER64: 16 hex digits or decimal values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
- _UNSIGNED _INTEGER64: 0 to 18446744073709551615.
- The maximum hexadecimal value for each numerical type is the maximum number of digits listed above, each valued at F.
- Convert hexadecimal to LONG values by appending the values with the suffix &. Example: &H8000 = -32768: &H8000& = 32768
- LONG 32 bit _RGB values can be made using hexadecimal values from &HFF000000 to &HFFFFFFFF with full _ALPHA only.
- LONG 32 bit _RGBA values can be made using hexadecimal values from &H00000000 to &HFFFFFFFF with any _ALPHA.
- Hexadecimal 0x is often used to prefix HEX$ port addresses in documentation. Replace 0x with &H in QB64 or QBasic.
- To convert hexadecimal strings returned from HEX$ with VAL you need to prefix the string with &H (for example, if the string is "FF" you should do VAL("&HFF") or VAL("&H" + hexvalue$).
Examples
- Example 1
- The maximum hexadecimal values of decimal value -1 in each numerical type are:
c&& = -1: d& = -1: e% = -1: f%% = -1 hx$ = HEX$(f%%) PRINT "Max hex _BYTE = "; hx$; " with"; LEN(hx$); "digits ="; VAL("&H" + hx$) hx$ = HEX$(e%) PRINT "Max hex INTEGER = "; hx$; " with"; LEN(hx$); "digits ="; VAL("&H" + hx$) hx$ = HEX$(d&) PRINT "Max hex LONG = "; hx$; " with"; LEN(hx$); "digits ="; VAL("&H" + hx$) hx$ = HEX$(c&&) PRINT "Max hex _INTEGER64 = "; hx$; " with"; LEN(hx$); "digits ="; VAL("&H" + hx$) hx$ = HEX$(9223372036854775807) PRINT "Max _INTEGER64 value = "; hx$; " with"; LEN(hx$); "digits" hx$ = HEX$(-9223372036854775808) PRINT "Min _INTEGER64 value = "; hx$; " with"; LEN(hx$); "digits" |
Max hex _BYTE = FF with 2 digits = 255 Max hex INTEGER = FFFF with 4 digits = 65535 Max hex LONG = FFFFFFFF with 8 digits = 4294967295 Max hex _INTEGER64 = FFFFFFFFFFFFFFFF with 16 digits =-1 Max _INTEGER64 value = 7FFFFFFFFFFFFFFF with 16 digits Min _INTEGER64 value = 8000000000000000 with 16 digits |
- Example 2
- Converting a decimal number to a binary string value using HEX$.
FUNCTION BIN$ (n&) h$ = HEX$(n&) 'get hexadecimal string value FOR i = 1 TO LEN(h$) 'scan the HEX$ digits SELECT CASE MID$(h$, i, 1) 'read each HEX$ digit CASE "0": b$ = b$ + "0000" CASE "1": b$ = b$ + "0001" CASE "2": b$ = b$ + "0010" CASE "3": b$ = b$ + "0011" CASE "4": b$ = b$ + "0100" CASE "5": b$ = b$ + "0101" CASE "6": b$ = b$ + "0110" CASE "7": b$ = b$ + "0111" CASE "8": b$ = b$ + "1000" CASE "9": b$ = b$ + "1001" CASE "A": b$ = b$ + "1010" CASE "B": b$ = b$ + "1011" CASE "C": b$ = b$ + "1100" CASE "D": b$ = b$ + "1101" CASE "E": b$ = b$ + "1110" CASE "F": b$ = b$ + "1111" END SELECT NEXT i b$ = RIGHT$(b$, LEN(b$) - INSTR(b$, "1") + 1) 'eliminate leading zeroes IF VAL(b$) THEN BIN$ = b$ ELSE BIN$ = "0" 'return zero if n& = 0 END FUNCTION |
- Explanation
- Hexadecimal digits can be any value up to 15 which also corresponds to all four bits on in binary. The function above just adds every four bit binary string value together to return the binary value. After they are concatenated, the leading bit on is found by INSTR and everything from that point is kept removing the leading "0"'s.
- Note
- Since QB64 v2.1 (incl. all QB64-PE versions) the built-in _BIN$ function should be used instead.
See also
- _BIN$, HEX$, OCT$, STR$
- &B (binary), &O (octal), VAL
- Base Comparisons
- HEX$ 32 Bit Values