Binary-Coded Decimal conversion

Discussion in 'Java' started by kelvSYC, Dec 30, 2003.

  1. kelvSYC

    kelvSYC Guest

    What is the best way to create a function that converts a BCD number
    (stored in an int to make things simple) into a normal integer (ie.
    int) value (or throw an exception if it cannot be done)?

    --
    I am only a mirage.
    kelvSYC, Dec 30, 2003
    #1
    1. Advertising

  2. kelvSYC

    Brad BARCLAY Guest

    kelvSYC wrote:
    > What is the best way to create a function that converts a BCD number
    > (stored in an int to make things simple) into a normal integer (ie.
    > int) value (or throw an exception if it cannot be done)?


    A straight mask, bit-rotate, and multiply, for each digit, followed by
    a summation of the results should suffice. You'll only need to throw an
    exception if the number you get from the mask and rotate is outside the
    acceptable BCD digit range (0 - 9). Presuming you're working with
    packed BCD for values with a maximum of 4-decimal-digits in length, the
    following should suffice:

    public static int BCDtoInt(int packedVal)
    throws NumberFormatException {
    int result = 0;
    int temp;
    int rot = 28;
    int mask = 0xF0000000;
    int mult = 10000000;

    while (rot>=0) {
    temp = (packedVal & mask) >> rot;

    if (temp > 9) throw new NumberFormatException();
    else result+=temp*mult;

    mult/=10;
    mask=mask>>>4;
    rot-=4;
    } // end-while

    return result;
    } // end-method

    Note that this can be optimized somewhat, but it should serve to
    illustrate the algorithm. HTH!

    Brad BARCLAY

    --
    =-=-=-=-=-=-=-=-=
    From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
    The jSyncManager Project: http://www.jsyncmanager.org
    Brad BARCLAY, Dec 30, 2003
    #2
    1. Advertising

  3. kelvSYC

    Brad BARCLAY Guest

    Brad BARCLAY wrote:

    > Presuming you're working with
    > packed BCD for values with a maximum of 4-decimal-digits in length...


    Oops -- I _meant_ to say "8-decimal-digits in length", and not 4. The
    code will happily handle 8 digit numbers -- my fingers simply hit the
    wrong numeric key in the above fragment :).

    Brad BARCLAY

    --
    =-=-=-=-=-=-=-=-=
    From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
    The jSyncManager Project: http://www.jsyncmanager.org
    
    Brad BARCLAY, Dec 30, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raja
    Replies:
    2
    Views:
    14,961
  2. lei
    Replies:
    1
    Views:
    579
    John W. Kennedy
    Dec 30, 2006
  3. Mars
    Replies:
    16
    Views:
    631
    Chris Williams
    Feb 22, 2005
  4. bob
    Replies:
    6
    Views:
    743
    Peter Shaggy Haywood
    Mar 21, 2006
  5. Ven
    Replies:
    3
    Views:
    1,316
Loading...

Share This Page