Hexadecimal

In BCX, the syntax to specify a hexadecimal number can use either BASIC syntax &H or C style 0x (zero x) preceding the hexadecimal string.

Example:

DIM aDecimal%

aDecimal% = &H1E240

PRINT aDecimal%

aDecimal% = 0x1E240

PRINT aDecimal%

PAUSE

Result:

123456
123456

👉 Please be aware that there can be ambiguity when BCX encounters a variable name that is preceded with &h or &H and fully qualifies as a BASIC hexadecimal number. An example is a variable declared as

DIM hDC AS HDC

and later in code appearing as

&hDC

Instead of being recognized as an address of a pointer, BCX processes it as a hexadecimal number and translates it to 0xdc.


HEX2DEC function

Purpose:

HEX2DEC takes a string argument representing a hexadecimal number and returns a 64-bit ULONGLONG integer.

👉 It is the programmer's responsibility to trap and remove all non-hex characters from the input string, otherwise the function will certainly yield incorrect results.

Syntax:

RetVal = HEX2DEC(HexString AS STRING)

Return Value:

  • RetVal 64-bit ULONGLONG integer value of HexString$.

Parameters:

  • Data type: STRING
    HexString Hexadecimal string to be converted to integer. The case-insensitive argument may take the following forms:
    • "&HFFFF" Hex string prepended with BASIC designation "&H" or "&h"
    • "0xFFFF" Hex string prepended with C designation "0x" or "0X"
    • "FFFF" Hex string

Example 1:

DIM AS ULONGLONG RetVal

RetVal = HEX2DEC("FFFFFFFFFFFFFFFF")

PRINT RetVal

PAUSE

Result:

18446744073709551615

Example 2:

PRINT USING$("###,###,###,###,###,###,###", HEX2DEC("&hFFFFFFFFFFFFFFFF"))

PRINT USING$("###,###,###,###,###,###,###", HEX2DEC("0xFFFFFFFFFFFFFFFF"))

PRINT USING$("###,###,###,###,###,###,###", HEX2DEC("FFFFFFFFFFFFFFFF"))

PAUSE

Result:

18,446,744,073,709,551,245
18,446,744,073,709,551,245
18,446,744,073,709,551,245

Press any key to continue . . .

HEX$ function

Purpose:

HEX$ converts an unsigned 64 bit integer to a hexadecimal string.

Syntax:

RetStr = HEX$(Number AS ULONGLONG [, Zeroed AS INTEGER])

Return Value:

  • Data type: STRING
    RetStr hexadecimal string of Number%.

Parameters:

  • Data type: ULONGLONG
    Number Unsigned 64 bit integer to be converted to hexadecimal string.
  • Data type: INTEGER
    Zeroed [OPTIONAL] Combined length of the computed hex string and prepended zeros. The maximum value allowed for the optional argument Zeroed is 16.

Example 1:

DIM RetStr$

RetStr$ = HEX$(65535)

PRINT RetStr$

PRINT HEX$(65535, 8)

PAUSE

Result:

FFFF
0000FFFF

Example 2:

'This example will print the hexadecimal
'codes for each of the characters in A$

DIM A$
DIM i%

A$ = "abcde"

FOR i%= 1 TO LEN(A$)
  PRINT HEX$(ASC(MID$(A$, i%, 1))) & " ";
NEXT i%
   
PAUSE

Result:

61 62 63 64 65

Example 3:

DIM HexTest AS UINT64
HexTest = 18446744073709551615ULL     ' Must append the ULL 
PRINT HEX$(HexTest)                   ' Result: FFFFFFFFFFFFFFFF 
PRINT HEX$((UINT64)2^64-1)            ' Result: 7FFFFFFFFFFFFFFF 

Result:

FFFFFFFFFFFFFFFF
7FFFFFFFFFFFFFFF

More hexadecimal to decimal conversion methods

Example 1:

Here is a short program that uses a hexadecimal to decimal conversion function written as a BCX macro utilizing the C function strtol. This example shows how to convert a hexadecimal string variable to a decimal.

MACRO HEXTODEC%(a) = strtol(a,NULL,16)

DIM aDecimal%
DIM aHexMainStr$

aHexMainStr$ = "1E240"
aDecimal% = HEXTODEC(aHexMainStr$)

PRINT aDecimal%

PAUSE

Result:

123456

Example 2:

Before BCX's HEX2DEC was given 64 bit capability, it could only handle a maximum of 32 bits "0xFFFFFFFF". The following routine extended HEX2DEC to 64 bits.

DIM RetValULL AS ULONGLONG
DIM sBuf$

RetValULL = Hx2DecULL("0xFFFFFFFFFFFFFFFF") ' 18446744073709551615 

sprintf(sBuf,"%llu",RetValULL)
PRINT sBuf$

PRINT USING$("###,###,###,###,###,###,###,###", RetValULL)

PAUSE

FUNCTION Hx2DecULL (h$) AS ULONGLONG

  DIM RAW hx$
  DIM RAW value AS ULONGLONG

  hx$ = LPAD$(REMAIN$(h$,"x"), 16, ASC("0"))

  value = ((ULONGLONG)HEX2DEC(LEFT$(hx$,8)) << 32)
  value|= (ULONG)HEX2DEC(RIGHT$(hx$,8))

  FUNCTION = value
END FUNCTION

Result:

18446744073709551615
18,446,744,073,709,551,616