Java BCD Convert To BCDToLong(byte[] bcdByte)

Here you can find the source of BCDToLong(byte[] bcdByte)

Description

BCD To Long

License

Open Source License

Declaration

public static long BCDToLong(byte[] bcdByte) 

Method Source Code

//package com.java2s;

public class Main {
    public static long BCDToLong(byte[] bcdByte) {
        if (bcdByte == null)
            return Integer.MIN_VALUE; //throw new NumberFormatException("bcdByte is null");
        return BCDToLong(bcdByte, 0, bcdByte.length);
    }// ww w .  j a v a  2s  .c  om

    public static long BCDToLong(byte[] buf, int offset, int length) {
        if (buf == null || offset < 0)
            return -1; // throw new NumberFormatException("bcdByte is null");
        if (length < 0 || buf.length < (offset + length))
            length = (buf.length - offset);
        int charPos = offset + length - 1;
        long result = 0;
        long j = 1;
        int high, low;

        if (length > 10) // Long.MAX_VALUE = 9223372036854775807
            length = 10;
        for (int i = 0; i < length; i++) {
            high = ((buf[charPos] >> 4) & 0x0F);
            low = (buf[charPos] & 0x0F);
            if (high > 9 || low > 9)
                return -2; // throw new NumberFormatException("Byte array contains non-digit");
            high = high * 10 + low;
            if (i == 9 && (high > 9 || (high == 9 && result > 223372036854775807L)))
                return -3; // throw new NumberFormatException("Number overflow");
            result = result + high * j;

            // really: j = (j * 100);
            j = ((j << 6) + (j << 5) + (j << 2));
            charPos--;
        }
        return result;
    }
}

Related

  1. bcd2Str(byte[] bytes)
  2. bcd2String(byte[] bytes)
  3. bcdNibbleToInt(byte b, boolean high)
  4. bcdToByte(byte src)
  5. BCDtoInt(int bcd)
  6. bcdToLong(byte[] src, int flag)
  7. bcdToString(byte[] bcd)