Proton BASIC Compiler - BCD to BIN array conversion

  • PicŪ Basic

  • BCD to BIN array conversion

    There are times when you have to enter a number via a keypad. The inputting of the number is a relatively easy affair using a number accumulator (See Password Controlled Entry Module on this site for more details). Then is often the case that you have to convert that array to a number.

    Presuming you entered the number highest first 1,2,3,4 for the number 1234, you array will hold the numbers 1,2,3,4 with 1 being at element 0 of the array.

    The following code will convert the array to a Binary number.

    For it to work the pointer has to be pointing to the last element of the array that holds a number. This is often the case on exiting the routine that loaded the array.

    Example Code

    Dim Multiplier as Dword
    Dim Result as Dword 
    Dim Pointer as Byte
    Multiplier = 10 ' The starting multiplier value 
    Result = Array[Pointer] ' Load in the lowest digit
    Repeat ' Loop
    Dec Pointer ' Move to next element 
    Result = Result + (Array[Pointer] * Multiplier)
    Multiplier = Multiplier * 10 ' Increase the multiplier
    Until Pointer = 0
    contributed by Timothy Box.