Java Integer to intToBcd(int src, int len, int flag)

Here you can find the source of intToBcd(int src, int len, int flag)

Description

int To Bcd

License

Apache License

Declaration

public static byte[] intToBcd(int src, int len, int flag) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {

    public static byte[] intToBcd(int src, int len, int flag) {
        return longToBcd(src, len, flag);
    }/*from  w w w  .  j  a va  2 s.c o  m*/

    public static byte[] longToBcd(long src, int len, int flag) {
        byte[] re = new byte[len];
        long tmp, high, low;
        if (src < 0)
            throw new RuntimeException(String.format("number: [%d] convert bcd error", src));

        for (int i = len - 1; i >= 0; i--) {
            if (src == 0)
                break;
            if (flag == 1) {
                tmp = src % 100;
                src /= 100;
                high = tmp / 10;
                low = tmp % 10;
            } else {
                tmp = src % 256;
                src /= 256;
                high = tmp / 16;
                low = tmp % 16;

            }
            re[i] = (byte) (high << 4 ^ low);

        }
        return re;
    }
}

Related

  1. intToAlgebraicLoc(int loc)
  2. intToAlpha(Integer no)
  3. intToASN1(byte[] d, int idx, int val)
  4. intToBase32(int n)
  5. intToBasicType(int i, Class clazz)
  6. intToBigEndian(int value, byte[] array, int index)
  7. intToBigEndianByteArray(int in)
  8. intToBlue(int color)
  9. IntToBuf2BE(int val, byte[] buf, int offset)