Yep, I know what you mean. I try to learn something every day, but
at my age that is getting a bit tougher.
Try my code. You will have to modify it to work with your Data section.
Sorry, I have to leave and don't have time to do it myself.
This code is accurate for 10 digits of my 14 digit precision field (which
most of my values fit in). There seems to be a problem with the ValF()
command beyond 11 digits of a string. Maybe you can verify that too.
Code: Select all
Procedure BCD_Convert(BegByte.w, Dec.w)
; Translate a BCD field to a float
Dim Value1.b(7)
Convert.s = ""
For I = 0 To 4 ; Temp read only 5 bytes
Value1(I) = PeekB(@recbuf + BegByte + I) ; Read 7 bytes to array
If Value1(I)
Convert.s = Convert + Hex(Value1(I)) ; Convert to a hex value
Else
Convert = Convert + Hex(Value1(I)) + Hex(Value1(I)) ; Gotta be 2 char
; This is necessary since Hex(0) returns "0" not "00" as expected.
EndIf
Next
Value1(7) = PeekB(@recbuf + BegByte + 7) ; Get Sign byte
LastByte.s = Hex(Value1(7)) ; Convert to a hex value
Decimal.w = Val(Mid(LastByte.s,2,1))
Sign1.w = 1
If Mid(LastByte.s,1,1) = "C"
Sign1 = -1
EndIf
; MessageRequester("Debug","Convert is: "+Convert+Chr(10)+"Decimal is: "+Str(Decimal)+Chr(10)+"Sign is: "+Str(Sign1)+Chr(10)+"Convert length is: "+Str(Len(Convert)),0)
Convert = Mid(Convert,0,Decimal) + "." + Mid(Convert,Decimal+1,Len(Convert)-Decimal)
; MessageRequester("Debug","Working is: "+Convert,0)
Results.f = ValF(StrF(ValF(Convert),Dec)) * Sign1
; MessageRequester("Debug","Result is: "+StrF(Results),0)
ProcedureReturn Results
EndProcedure
testing some negative numbers after a brief holiday over the weekend.
I am sure you can probably improve this code. I am still pretty much
a novice with PB, so it may not be the best way to do things.
Regards,
Terry
