udfByteToHex
udfHexToNum
udfHexToByte
udfHexToDec
udfHexToFloat
udfDecToHex
str udfByteToHexV1 (int)
str udfByteToHexV2 (int)
str udfByteToHexV3 (int)
int udfHexToNum (str)
int udfHexToByte (str)
int udfHexToDec (str)
flt udfHexToFloat (str)
str udfDecToHexV1 (int)
str udfDecToHexV2 (int)
str udfDecToHexV3 (int, int, bln)
str udfDecToHexV4 (int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfByteToHexV1 (intByte) ; intByte = 0..255.
strHexDigits = "0123456789ABCDEF" ; strHexDigits = "0123456789abcdef".
Return StrSub (strHexDigits, 1 + (intByte >> 4), 1) : StrSub (strHexDigits, 1 + (intByte & 15), 1)
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfByteToHexV2 (intByte) ; intByte = 0..255.
Return Num2Char ((intByte >> 4) + 48 + 7 * ((intByte >> 4) > 9)) : Num2Char ((intByte & 15) + 48 + 7 * ((intByte & 15) > 9)) ; Uppercase strHexDigits = "0123456789ABCDEF".
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfByteToHexV3 (intByte) ; intByte = 0..255.
Return Num2Char ((intByte >> 4) + 48 + 39 * ((intByte >> 4) > 9)) : Num2Char ((intByte & 15) + 48 + 39 * ((intByte & 15) > 9)) ; Lowercase strHexDigits = "0123456789abcdef".
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfHexToNum (strHex1)
intNum = Char2Num (StrUpper (strHex1)) - 48
Return intNum - 7 * (intNum > 9)
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfHexToByte (strHex2) ; strHex2 must be StrByteCount(strHex2) == 2.
strHex2 = StrUpper (StrTrim (strHex2))
intNum1 = Char2Num (StrSub (strHex2, 1, 1)) - 48
intNum2 = Char2Num (StrSub (strHex2, 2, 1)) - 48
Return ((intNum1 - 7 * (intNum1 > 9)) << 4) + (intNum2 - 7 * (intNum2 > 9))
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfHexToDec (strHex)
strHexDigits = "0123456789ABCDEF"
strHex = StrUpper (StrTrim (strHex))
intLen = StrLen (strHex)
intDec = 0
For intI = 1 To intLen
   intDec = (intDec << 4) + StrIndex (strHexDigits, StrSub (strHex, intI, 1), 0, @FWDSCAN) - 1
Next
Return intDec ; Note: Returned negative numbers are ok for usage in WinBatch.
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfHexToFloat (strHex)
strHexDigits = "0123456789ABCDEF"
strHex = StrUpper (StrTrim (strHex))
intLen = StrLen (strHex)
fltDec = 0.0
For intI = 1 To intLen
   fltDec = (fltDec * 16) + StrIndex (strHexDigits, StrSub (strHex, intI, 1), 0, @FWDSCAN) - 1
Next
Return fltDec
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfDecToHexV1 (intDecimal)
strHexDigits = "0123456789ABCDEF"
strHex = ""
intZ = 1
For i = 7 To 0 By -1
   intN = (intDecimal >> (i * 4)) & 15
   If intN == 0 && intZ == 1 Then Continue
   intZ = 0
   strHex = strHex : StrSub (strHexDigits, intN + 1, 1)
Next
Return strHex
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfDecToHexV2 (intDecimal)
strHexDigits = "0123456789ABCDEF"
strHex = ""
intZ = 1
For i = 7 To 0 By -1
   intN = (intDecimal >> (i * 4)) & 15
   If !intN Then If intZ Then Continue
   intZ = 0
   strHex = strHex : StrSub (strHexDigits, intN + 1, 1)
Next
Return strHex
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfDecToHexV3 (intDecimal, intPadLength, blnCaseMode)
intPadLength = Min (8, Max (1, intPadLength))
strHexDigits = "0123456789ABCDEF"
If !blnCaseMode Then strHexDigits = "0123456789abcdef"
strHex = ""
intZ = 1
For i = 7 To 0 By -1
   intN = (intDecimal >> (i * 4)) & 15
   If !intN Then If intZ Then Continue
   intZ = 0
   strHex = strHex : StrSub (strHexDigits, intN + 1, 1)
Next
strHex = StrFixLeft (strHex, "0", intPadLength)
Return strHex
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfDecToHexV4 (intDecimal) ; Negative numbers are allowed too.
strHex = ""
While @TRUE
   strHex = StrSub ("0123456789ABCDEF", (intDecimal & 15) + 1, 1) : strHex
   intDecimal = (intDecimal >> 4) & 268435455
   If !intDecimal Then Return strHex
EndWhile
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



;------------------------------------------------------------------------------------------------------------------------------------------
; Test.

Message ("Demo udfHexToNum (hexchar)", 'What decimal number is "D" ?' : @LF : udfHexToNum ("D"))

Message ("Demo udfByteToHex (intByte)", 'Who knows the magic of number 221?' : @LF : udfByteToHexV2 (221) : ' who else?')
Message ("Demo udfHexToByte (strHex)", 'What is the number of "DD"?' : @LF : udfHexToByte ('DD'))

Message ("Demo udfHexToDec (strHex)", "FF" : " = " : udfHexToDec ("FF"))
Message ("Demo udfHexToDec (strHex)", "F8000000" : " = " : udfHexToDec ("F8000000"))

Message ("Demo udfHexToFloat (strHex)", "F8000000" : " = " : udfHexToFloat ("F8000000"))

Message ("Demo udfDecToHex (decimal)", "Who did first programming?" : @LF : udfDecToHexV1 (2778))

Message ("Demo udfDecToHex (decimal_neg)", "Negative number to hex string." : @LF : udfDecToHexV4 (-2778)) ; "FFFFF526"

;------------------------------------------------------------------------------------------------------------------------------------------

:Test_a1
strHex = ""
Exclusive (@ON)
intTicksStart = GetTickCount ()
For intByte = 0 To 255
   strHex = strHex : udfByteToHexV1 (intByte) : ","
Next
intTicks1 = GetTickCount () - intTicksStart
Exclusive (@OFF)
Message ("Demo udfByteToHexV1 (intByte)", strHex)


:Test_a2
strHex = ""
Exclusive (@ON)
intTicksStart = GetTickCount ()
For intByte = 0 To 255
   strHex = strHex : udfByteToHexV2 (intByte) : ","
Next
intTicks2 = GetTickCount () - intTicksStart
Exclusive (@OFF)
Message ("Demo udfByteToHexV2 (intByte)", strHex)


:Result_a
intMaxTicks = Max (intTicks1, intTicks2)
strMsgTitle = "Demo udfByteToHex"
strMsgText = "udfByteToHexV1" : @TAB : "intTicks=" : intTicks1 : @TAB : 100 * intTicks1 / intMaxTicks : "%%" : @LF
strMsgText = strMsgText : "udfByteToHexV2" : @TAB : "intTicks=" : intTicks2 : @TAB : 100 * intTicks2 / intMaxTicks : "%%" : @LF
Message (strMsgTitle, strMsgText)

;------------------------------------------------------------------------------------------------------------------------------------------

:Test_b1
; Uses strHex from Test_a2.
strBytes = ""
Exclusive (@ON)
intTicksStart = GetTickCount ()
For intI = 1 To 256
   strBytes = strBytes : udfHexToByte (ItemExtract (intI, strHex, ",")) : ","
Next
intTicks1 = GetTickCount () - intTicksStart
Exclusive (@OFF)
Message ("Demo udfHexToByte (strHex)", strBytes)


:Test_b2
; Uses strHex from Test_a2.
strBytes = ""
Exclusive (@ON)
intTicksStart = GetTickCount ()
For intI = 1 To 256
   strBytes = strBytes : udfHexToDec (ItemExtract (intI, strHex, ",")) : ","
Next
intTicks2 = GetTickCount () - intTicksStart
Exclusive (@OFF)
Message ("Demo udfHexToDec (strHex)", strBytes)


:Result_b
intMaxTicks = Max (intTicks1, intTicks2)
strMsgTitle = "Demo udfHexToByte"
strMsgText = "udfHexToByte" : @TAB : "intTicks=" : intTicks1 : @TAB : 100 * intTicks1 / intMaxTicks : "%%" : @LF
strMsgText = strMsgText : "udfHexToDec" : @TAB : "intTicks=" : intTicks2 : @TAB : 100 * intTicks2 / intMaxTicks : "%%" : @LF
Message (strMsgTitle, strMsgText)

;------------------------------------------------------------------------------------------------------------------------------------------

:Test_c1
Exclusive (@ON)
intTicksStart = GetTickCount ()
For intI = 1 To 20
   strHex = udfDecToHexV1 (19541201)
Next
intTicks1 = GetTickCount () - intTicksStart
Exclusive (@OFF)
Message ("Demo  udfDecToHexV1 (intDecimal)", strHex)


:Test_c2
Exclusive (@ON)
intTicksStart = GetTickCount ()
For intI = 1 To 20
   strHex = udfDecToHexV2 (19541201)
Next
intTicks2 = GetTickCount () - intTicksStart
Exclusive (@OFF)
Message ("Demo  udfDecToHexV2 (intDecimal)", strHex)


:Test_c3
Exclusive (@ON)
intTicksStart = GetTickCount ()
For intI = 1 To 20
   strHex = udfDecToHexV3 (19541201, 8, 1)
Next
intTicks3 = GetTickCount () - intTicksStart
Exclusive (@OFF)
Message ("Demo  udfDecToHexV3 (intDecimal, intPadLength, blnCaseMode)", strHex)


:Test_c4
Exclusive (@ON)
intTicksStart = GetTickCount ()
For intI = 1 To 20
   strHex = udfDecToHexV4 (19541201)
Next
intTicks4 = GetTickCount () - intTicksStart
Exclusive (@OFF)
Message ("Demo  udfDecToHexV4 (intDecimal)", strHex)


:Result_c
intMaxTicks = Max (intTicks1, intTicks2, intTicks3, intTicks4)
strMsgTitle = "Demo udfDecToHex"
strMsgText = "udfDecToHexV1" : @TAB : "intTicks=" : intTicks1 : @TAB : 100 * intTicks1 / intMaxTicks : "%%"
strMsgText = strMsgText : @LF : "udfDecToHexV2" : @TAB : "intTicks=" : intTicks2 : @TAB : 100 * intTicks2 / intMaxTicks : "%%"
strMsgText = strMsgText : @LF : "udfDecToHexV3" : @TAB : "intTicks=" : intTicks3 : @TAB : 100 * intTicks3 / intMaxTicks : "%%"
strMsgText = strMsgText : @LF : "udfDecToHexV4" : @TAB : "intTicks=" : intTicks4 : @TAB : 100 * intTicks4 / intMaxTicks : "%%"
Message (strMsgTitle, strMsgText)

Exit
;------------------------------------------------------------------------------------------------------------------------------------------
; udfByteToHexV1 (intByte)
; udfByteToHexV2 (intByte)
; udfByteToHexV3 (intByte)
; udfHexToNum (strHex1)
; udfHexToByte (strHex2)
; udfHexToDec (strHex)
; udfHexToFloat (strHex)
; udfDecToHexV1 (intDecimal)
; udfDecToHexV2 (intDecimal)
; udfDecToHexV3 (intDecimal, intPadLength, blnCaseMode)
; udfDecToHexV4 (intDecimal)
;------------------------------------------------------------------------------------------------------------------------------------------
; (c)Detlev Dalitz.20020831.20100207.20100518.
;------------------------------------------------------------------------------------------------------------------------------------------