Author Topic: BCD to decimal  (Read 1276 times)

Offline Gary

  • Dr. Type
  • ****
  • Posts: 313
    • View Profile
BCD to decimal
« on: 2011-Jul-05 »
Not sure if anyone still uses Binary Coded Decimal but I needed to write a BCD to Decimal conversion for my project and thought I would share the code with anyone that might need it

Code: GLBasic [Select]
FUNCTION bcdbin32:inval

        LOCAL weight_lut[]
        LOCAL   weight, retval, i
        DIMDATA weight_lut[], 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000

        retval = 0

        FOR i=0 TO 7
                weight = weight_lut[i]
                WHILE (BAND(inval,15))
                        retval = retval + weight
                        inval = inval - 1
                inval = ASR(inval, 4)

        RETURN retval


Now I fully expect someone to point out there is a built in function that I have missed lol

Hope its useful for someone