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.
DIM aDecimal% aDecimal% = &H1E240 PRINT aDecimal% aDecimal% = 0x1E240 PRINT aDecimal% PAUSE
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 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:
Parameters:
|
DIM AS ULONGLONG RetVal RetVal = HEX2DEC("FFFFFFFFFFFFFFFF") PRINT RetVal PAUSE
18446744073709551615
PRINT USING$("###,###,###,###,###,###,###", HEX2DEC("&hFFFFFFFFFFFFFFFF")) PRINT USING$("###,###,###,###,###,###,###", HEX2DEC("0xFFFFFFFFFFFFFFFF")) PRINT USING$("###,###,###,###,###,###,###", HEX2DEC("FFFFFFFFFFFFFFFF")) PAUSE
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$ converts an unsigned 64 bit integer to a hexadecimal string.
Syntax:RetStr = HEX$(Number AS ULONGLONG [, Zeroed AS INTEGER]) Return Value:
Parameters: |
DIM RetStr$ RetStr$ = HEX$(65535) PRINT RetStr$ PRINT HEX$(65535, 8) PAUSE
FFFF 0000FFFF
'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
61 62 63 64 65
DIM HexTest AS UINT64 HexTest = 18446744073709551615ULL ' Must append the ULL PRINT HEX$(HexTest) ' Result: FFFFFFFFFFFFFFFF PRINT HEX$((UINT64)2^64-1) ' Result: 7FFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFF 7FFFFFFFFFFFFFFF
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
123456
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
18446744073709551615 18,446,744,073,709,551,616