//--------------------------------------------------------------------------
FUNCTION HexToBinary(cHex)
LOCAL nLenBin, nX, nPos, cBinNumber, cCharHex, cBinBase
cBinNumber := ""
cHex := alltrim(cHex)
FOR nX := len(cHex) TO 1 STEP -1
cCharHex := substr(cHex, -nX, 1)
nPos := ascan(aHexBinary, { |a| a[1] == substr(cHex, -nX, 1)} )
cBinBase := aHexBinary[nPos,2]
cBinNumber := cBinNumber + cBinBase
NEXT
RETURN (cBinNumber)
//--------------------------------------------------------------------------
FUNCTION BinaryToHex(cBin)
LOCAL nLenBin, nX, nPos, cBinNumber, cHex := ""
cBin := alltrim(cBin)
cBin := atrepl(" ", cBin, "")
nLenBin := len(cBin)
aBinaryNumber := {}
// Validate the binary number
DO CASE
CASE nLenBin <= 4
cBin := "000"+cBin
cBin := right(cBin,4)
CASE nLenBin <= 8
cBin := "000"+cBin
cBin := right(cBin,8)
CASE nLenBin <= 12
cBin := "000"+cBin
cBin := right(cBin,12)
CASE nLenBin <= 16
cBin := "000"+cBin
cBin := right(cBin,16)
CASE nLenBin <= 20
cBin := "000"+cBin
cBin := right(cBin,20)
CASE nLenBin <= 24
cBin := "000"+cBin
cBin := right(cBin,24)
CASE nLenBin <= 28
cBin := "000"+cBin
cBin := right(cBin,28)
CASE nLenBin <= 32
cBin := "000"+cBin
cBin := right(cBin,32)
CASE nLenBin <= 36
cBin := "000"+cBin
cBin := right(cBin,36)
CASE nLenBin <= 40
cBin := "000"+cBin
cBin := right(cBin,40)
CASE nLenBin <= 44
cBin := "000"+cBin
cBin := right(cBin,44)
CASE nLenBin <= 48
cBin := "000"+cBin
cBin := right(cBin,48)
CASE nLenBin <= 52
cBin := "000"+cBin
cBin := right(cBin,52)
CASE nLenBin <= 56
cBin := "000"+cBin
cBin := right(cBin,56)
CASE nLenBin <= 60
cBin := "000"+cBin
cBin := right(cBin,60)
CASE nLenBin <= 64
cBin := "000"+cBin
cBin := right(cBin,64)
ENDCASE
// Create Array of Binary
nLenBin := len(cBin)
FOR nX := nLenBin to 1 step -4
cBinNumber := substr(cBin, -nX, 4)
aadd(aBinaryNumber, cBinNumber)
NEXT
// Convert Binary to Hexadecimal
nLenBin := len(aBinaryNumber)
FOR nX := 1 TO nLenBin
cBinNumber := aBinaryNumber[nX]
// Search Hex for this Binary
nPos := aScan(aBinary, cBinNumber,,,.t. )
IIF( nPos > 0, cHex := cHex + aHex[nPos], msginfo(cBinNumber+ " not found") )
NEXT
RETURN ("0X"+cHex)
//--------------------------------------------------------------------------
FUNCTION DecToHex(nDec)
// Use NumToHex() from harbour
RETURN (.T.)
//--------------------------------------------------------------------------
FUNCTION HexToDecimal(cHex)
LOCAL nDec := 0, nX, nLen, cHexVal, nDecVal
cHex := alltrim(cHex)
cHex := atrepl("0X", cHex, "")
nLen := len(cHex)
FOR nX := 1 TO nLen
cHexVal := substr(cHex, -nX, 1)
DO CASE
CASE cHexVal = "A"
nDecVal := 10
CASE cHexVal = "B"
nDecVal := 11
CASE cHexVal = "C"
nDecVal := 12
CASE cHexVal = "D"
nDecVal := 13
CASE cHexVal = "E"
nDecVal := 14
CASE cHexVal = "F"
nDecVal := 15
OTHERWISE
nDecVal := val(cHexVal)
ENDCASE
nDec := nDec + nDecVal * 16 ** (nX-1)
nDec := int(nDec)
NEXT
RETURN (nDec)
//--------------------------------------------------------------------------
FUNCTION Hex2Dec(cHex)
LOCAL nDec := 1, JJ, nLen := len(cHex)
FOR jj := 1 TO nLen
nDec := nDec*16 + at(substr(cHex, jj, 1), "123456789ABCDEF")
NEXT
RETURN (nDec)