&H: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(10 intermediate revisions by the same user not shown)
Line 2: Line 2:




{{PageSyntax}}  
{{PageSyntax}}
: {{Parameter|a&}} = [[&H]]FACE
: {{Parameter|a&}} = [[&H]]C0DEBA5E




{{PageDescription}}  
{{PageDescription}}
* 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.
* 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.
* Leading zero values can be omitted just like in decimal values as they add nothing to the return value.
Line 13: Line 13:
:* [[INTEGER]]: 4 hex digits or a decimal value range from -32,768 to 32,767. [[_UNSIGNED]]: 0 to 65535.
:* [[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.
:* [[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.  
:* [[_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.
:* [[_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'''.
* 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
* Convert hexadecimal to [[LONG]] values by appending the values with the suffix '''&'''. Example: [[&H]]8000 = -32768: [[&H]]8000'''&''' = 32768
* [[LONG]] 32 bit [[_RGB]] values can be made using hexadecimal values from '''&HFF{{Text|00|red}}{{Text|00|green}}{{Text|00|blue}}''' to '''&HFF{{Text|FF|red}}{{Text|FF|green}}{{Text|FF|blue}}''' with full [[_ALPHA]] only.
* [[LONG]] 32 bit [[_RGB]] values can be made using hexadecimal values from '''&HFF{{Text|00|red}}{{Text|00|green}}{{Text|00|blue}}''' to '''&HFF{{Text|FF|red}}{{Text|FF|green}}{{Text|FF|blue}}''' with full [[_ALPHA]] only.
* [[LONG]] 32 bit [[_RGBA]] values can be made using hexadecimal values from '''&H00{{Text|00|red}}{{Text|00|green}}{{Text|00|blue}}''' to '''&HFF{{Text|FF|red}}{{Text|FF|green}}{{Text|FF|blue}}''' with any [[_ALPHA]].
* [[LONG]] 32 bit [[_RGBA]] values can be made using hexadecimal values from '''&H00{{Text|00|red}}{{Text|00|green}}{{Text|00|blue}}''' to '''&HFF{{Text|FF|red}}{{Text|FF|green}}{{Text|FF|blue}}''' with any [[_ALPHA]].
* Hexadecimal '''0x''' is often used to prefix [[HEX$]] port addresses in documentation. Replace '''0x''' with [[&H]] in QB64 or QBasic.
* Hexadecimal '''0x''' is often used to prefix [[HEX$]] port addresses in documentation. Replace '''0x''' with [[&H]] in QB64 or QBasic.
* To convert hex 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$).
* 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 {{InlineCode}}{{Cl|VAL}}("&HFF"){{InlineCodeEnd}} or {{InlineCode}}{{Cl|VAL}}("&H" + hexvalue$){{InlineCodeEnd}}.




{{PageExamples}}  
{{PageExamples}}
;Example 1:The maximum octal values of decimal value -1 in each numerical type are:
;Example 1:The maximum hexadecimal values of decimal value -1 in each numerical type are:
{{CodeStart}}
{{CodeStart}}
c&& = -1: d& = -1: e% = -1: f%% = -1
c&& = -1: d& = -1: e% = -1: f%% = -1
Line 38: Line 38:
{{Cl|PRINT}} "Max {{Cl|_INTEGER64}} value = "; hx$; " with"; {{Cl|LEN}}(hx$); "digits"
{{Cl|PRINT}} "Max {{Cl|_INTEGER64}} value = "; hx$; " with"; {{Cl|LEN}}(hx$); "digits"
hx$ = {{Cl|HEX$}}(-9223372036854775808)
hx$ = {{Cl|HEX$}}(-9223372036854775808)
{{Cl|PRINT}} "Min {{Cl|_INTEGER64}} value = "; hx$; " with"; {{Cl|LEN}}(hx$); "digits"  
{{Cl|PRINT}} "Min {{Cl|_INTEGER64}} value = "; hx$; " with"; {{Cl|LEN}}(hx$); "digits"
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}Max hex _BYTE = FF with 2 digits = 255
{{OutputStart}}
Max hex _BYTE = FF with 2 digits = 255
Max hex INTEGER = FFFF with 4 digits = 65535
Max hex INTEGER = FFFF with 4 digits = 65535
Max hex LONG = FFFFFFFF with 8 digits = 4294967295
Max hex LONG = FFFFFFFF with 8 digits = 4294967295
Line 51: Line 52:
;Example 2:Converting a decimal number to a binary string value using [[HEX$]].
;Example 2:Converting a decimal number to a binary string value using [[HEX$]].
{{CodeStart}}
{{CodeStart}}
{{Cl|FUNCTION}} BIN$ (n&)
{{Cl|FUNCTION}} BinStr$ (n&)
   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"
Line 74: Line 75:
   {{Cl|NEXT}} i
   {{Cl|NEXT}} i
   b$ = {{Cl|RIGHT$}}(b$, {{Cl|LEN}}(b$) - {{Cl|INSTR}}(b$, "1") + 1)  'eliminate leading zeroes
   b$ = {{Cl|RIGHT$}}(b$, {{Cl|LEN}}(b$) - {{Cl|INSTR}}(b$, "1") + 1)  'eliminate leading zeroes
   {{Cl|IF}} {{Cl|VAL}}(b$) {{Cl|THEN}} BIN$ = b$ {{Cl|ELSE}} BIN$ = "0"      'return zero if n& = 0
   {{Cl|IF}} {{Cl|VAL}}(b$) {{Cl|THEN}} BinStr$ = b$ {{Cl|ELSE}} BinStr$ = "0"      'return zero if n& = 0
{{Cl|END FUNCTION}}  
{{Cl|END FUNCTION}}
{{CodeEnd}}
{{CodeEnd}}
{{small|Code by CodeGuy}}
{{Small|Code by CodeGuy}}
;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.
;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 2.1 (QB64 Team) respectively QBPE 0.5 (QB64 Phoenix Edition) the built-in [[_BIN$]] function should be used instead.
;Note: Since QB64 v2.1 (incl. all QB64-PE versions) the built-in [[_BIN$]] function should be used instead.


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




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 21:18, 5 April 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 BinStr$ (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 BinStr$ = b$ ELSE BinStr$ = "0"       'return zero if n& = 0
END FUNCTION
Code by CodeGuy
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



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