Java Long to longToBcd(long num, int size)

Here you can find the source of longToBcd(long num, int size)

Description

long To Bcd

License

Open Source License

Declaration

public static byte[] longToBcd(long num, int size) 

Method Source Code

//package com.java2s;
/**/*from   ww  w. j  a  v a  2s  .  com*/
 * ****************************************************************************
 * Copyright (c) 2015, MasterCard International Incorporated and/or its
 * affiliates. All rights reserved.
 * <p/>
 * The contents of this file may only be used subject to the MasterCard
 * Mobile Payment SDK for MCBP and/or MasterCard Mobile MPP UI SDK
 * Materials License.
 * <p/>
 * Please refer to the file LICENSE.TXT for full details.
 * <p/>
 * TO THE EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
 * WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON INFRINGEMENT. TO THE EXTENT PERMITTED BY LAW, IN NO EVENT SHALL
 * MASTERCARD OR ITS AFFILIATES BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 * *****************************************************************************
 */

public class Main {
    public static byte[] longToBcd(long num, int size) {

        int digits = 0;

        long temp = num;
        while (temp != 0) {
            digits++;
            temp /= 10;
        }

        int byteLen = digits % 2 == 0 ? digits / 2 : (digits + 1) / 2;
        boolean isOdd = digits % 2 != 0;

        byte bcd[] = new byte[byteLen];

        for (int i = 0; i < digits; i++) {
            byte tmp = (byte) (num % 10);

            if (i == digits - 1 && isOdd) {
                bcd[i / 2] = tmp;
            } else if (i % 2 == 0) {
                bcd[i / 2] = tmp;
            } else {
                byte foo = (byte) (tmp << 4);
                bcd[i / 2] |= foo;
            }

            num /= 10;
        }

        for (int i = 0; i < byteLen / 2; i++) {
            byte tmp = bcd[i];
            bcd[i] = bcd[byteLen - i - 1];
            bcd[byteLen - i - 1] = tmp;
        }

        if (size == byteLen) {
            return bcd;
        } else {
            byte[] ret = new byte[size];
            System.arraycopy(bcd, 0, ret, size - byteLen, byteLen);
            return ret;
        }
    }
}

Related

  1. longTo4ByteArray(byte[] bytes, int offset, long value)
  2. longTo4LengthBytes(long num)
  3. LongToAscii(long number, byte[] buf, int offset, int length)
  4. longToBaseCode(char[] target, int targetOffset, long value, int base, int lengthLimit, boolean fillZeros, boolean upperCase)
  5. longToBasicType(long l, Class clazz)
  6. longToBcd(long src, int len, int flag)
  7. longToBigEndian(long value, byte[] in, int offset)
  8. longToBinary(long num)
  9. longtobinarystring(long wert, int bits)