udfBcdToDec
int udfBcdToDecV1 (hdlBB)
int udfBcdToDecV2 (hdlBB)
;----------------------------------------------------------------------------------------
#DefineFunction udfBcdToDecV1 (hdlBB) ; The String Version.
strNumber = ""
intCount = BinaryEodGet (hdlBB) - 2
For intI = 0 To intCount
   intByte = BinaryPeek (hdlBB, intI)
   strNumber = strNumber : (intByte >> 4) ; High nibble.
   strNumber = strNumber : (intByte & 15) ; Low  nibble.
Next
intByte = BinaryPeek (hdlBB, intCount + 1)
strNumber = strNumber : (intByte >> 4)
If (intByte & 15) == 13 Then Return 0 - strNumber
Return 0 + strNumber
;..........................................................................................................................................
; This UDF "udfBcdToDec" returns an integer value.
; The BCD number to convert is presented in a binary buffer.
; Detlev Dalitz.20020131.20100204.
;..........................................................................................................................................
#EndFunction
;----------------------------------------------------------------------------------------


;----------------------------------------------------------------------------------------
#DefineFunction udfBcdToDecV2 (hdlBB) ; The Number Version.
intNumber = 0
intCount = BinaryEodGet (hdlBB) - 2
For intI = 0 To intCount
   intByte = BinaryPeek (hdlBB, intI)
   intNumber = 10 * intNumber + (intByte >> 4) ; High nibble.
   intNumber = 10 * intNumber + (intByte & 15) ; Low  nibble.
Next
intByte = BinaryPeek (hdlBB, intCount + 1)
intNumber = 10 * intNumber + (intByte >> 4)
If (intByte & 15) == 13 Then Return -intNumber
Return intNumber
;..........................................................................................................................................
; This UDF "udfBcdToDec" returns an integer value.
; The BCD number to convert is presented in a binary buffer.
; Detlev Dalitz.20020131.20100204.
;..........................................................................................................................................
#EndFunction
;----------------------------------------------------------------------------------------


; Test.

:Test1
; Poke decimal number -7 into buffer.
strNumber = "-7"
hdlBB = BinaryAlloc (1)    ;  ==> "7D".
BinaryPoke (hdlBB, 0, 125) ; (7) * 16 + 13(-).
Message ("Demo  udfBcdToDecV1 (hdlBB)  The String Version", strNumber : " = " : udfBcdToDecV1 (hdlBB))
Message ("Demo  udfBcdToDecV2 (hdlBB)  The Number Version", strNumber : " = " : udfBcdToDecV2 (hdlBB))
BinaryFree (hdlBB)


:Test2
; Poke decimal number -4321 into buffer.
strNumber = "-4321"
hdlBB = BinaryAlloc (3)    ; ==> "04321D".
BinaryPoke (hdlBB, 0, 4)   ;  0  * 16 + (4)
BinaryPoke (hdlBB, 1, 50)  ; (3) * 16 + (2)
BinaryPoke (hdlBB, 2, 29)  ; (1) * 16 + 13(-)
Message ("Demo  udfBcdToDecV1 (hdlBB)  The String Version", strNumber : " = " : udfBcdToDecV1 (hdlBB))
Message ("Demo  udfBcdToDecV2 (hdlBB)  The Number Version", strNumber : " = " : udfBcdToDecV2 (hdlBB))
BinaryFree (hdlBB)


:Test3
; Poke decimal number +000050 into buffer.
strNumber = "+000050"
hdlBB = BinaryAlloc (4)    ; ==> "0000050C".
BinaryPoke (hdlBB, 0, 0)   ;
BinaryPoke (hdlBB, 1, 0)   ;
BinaryPoke (hdlBB, 2, 5)   ;  0  * 16 + (5).
BinaryPoke (hdlBB, 3, 12)  ; (0) * 16 + 12(+).
Message ("Demo  udfBcdToDecV1 (hdlBB)  The String Version", strNumber : " = " : udfBcdToDecV1 (hdlBB))
Message ("Demo  udfBcdToDecV2 (hdlBB)  The Number Version", strNumber : " = " : udfBcdToDecV2 (hdlBB))
BinaryFree (hdlBB)


:Test4
Display (1, "Demo  udfBcdToDec (hdlBB)", "Performance Test, please wait ...")
hdlBB = BinaryAlloc (3)
BinaryPoke (hdlBB, 0, 4)
BinaryPoke (hdlBB, 1, 50)
BinaryPoke (hdlBB, 2, 29)

intLoop = 1000
Exclusive (@ON)
intTicksStart = GetTickCount ()
For intL = 1 To intLoop
   intNumber = udfBcdToDecV1 (hdlBB)
Next
intTicksS = GetTickCount () - intTicksStart

intTicksStart = GetTickCount ()
For intL = 1 To intLoop
   intNumber = udfBcdToDecV2 (hdlBB)
Next
intTicksN = GetTickCount () - intTicksStart
Exclusive (@OFF)

Decimals (0)
intTicksMax = Max (intTicksS, intTicksN)
intPctS = 100.0 * intTicksS / intTicksMax
intPctN = 100.0 * intTicksN / intTicksMax

strMsgText = ""
strMsgText = strMsgText : "intTicksS = " : @TAB : intTicksS : @TAB : intPctS : " %%" : @CRLF
strMsgText = strMsgText : "intTicksN = " : @TAB : intTicksN : @TAB : intPctN : " %%"
strMsgTitle = "Demo  udfBcdToDec (hdlBB)  Performance Test"
Message (strMsgTitle, strMsgText)

BinaryFree (hdlBB)
Exit