Java Long to LongToAscii(long number, byte[] buf, int offset, int length)

Here you can find the source of LongToAscii(long number, byte[] buf, int offset, int length)

Description

Long To Ascii

License

Open Source License

Declaration

public static int LongToAscii(long number, byte[] buf, int offset, int length) 

Method Source Code

//package com.java2s;

public class Main {
    public final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
            'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
            'z' };
    public final static byte[] BCD_Digit = { (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
            (byte) 0x05, (byte) 0x06, (byte) 0x07, (byte) 0x08, (byte) 0x09, (byte) 0x10, (byte) 0x11, (byte) 0x12,
            (byte) 0x13, (byte) 0x14, (byte) 0x15, (byte) 0x16, (byte) 0x17, (byte) 0x18, (byte) 0x19, (byte) 0x20,
            (byte) 0x21, (byte) 0x22, (byte) 0x23, (byte) 0x24, (byte) 0x25, (byte) 0x26, (byte) 0x27, (byte) 0x28,
            (byte) 0x29, (byte) 0x30, (byte) 0x31, (byte) 0x32, (byte) 0x33, (byte) 0x34, (byte) 0x35, (byte) 0x36,
            (byte) 0x37, (byte) 0x38, (byte) 0x39, (byte) 0x40, (byte) 0x41, (byte) 0x42, (byte) 0x43, (byte) 0x44,
            (byte) 0x45, (byte) 0x46, (byte) 0x47, (byte) 0x48, (byte) 0x49, (byte) 0x50, (byte) 0x51, (byte) 0x52,
            (byte) 0x53, (byte) 0x54, (byte) 0x55, (byte) 0x56, (byte) 0x57, (byte) 0x58, (byte) 0x59, (byte) 0x60,
            (byte) 0x61, (byte) 0x62, (byte) 0x63, (byte) 0x64, (byte) 0x65, (byte) 0x66, (byte) 0x67, (byte) 0x68,
            (byte) 0x69, (byte) 0x70, (byte) 0x71, (byte) 0x72, (byte) 0x73, (byte) 0x74, (byte) 0x75, (byte) 0x76,
            (byte) 0x77, (byte) 0x78, (byte) 0x79, (byte) 0x80, (byte) 0x81, (byte) 0x82, (byte) 0x83, (byte) 0x84,
            (byte) 0x85, (byte) 0x86, (byte) 0x87, (byte) 0x88, (byte) 0x89, (byte) 0x90, (byte) 0x91, (byte) 0x92,
            (byte) 0x93, (byte) 0x94, (byte) 0x95, (byte) 0x96, (byte) 0x97, (byte) 0x98, (byte) 0x99, };

    public static int LongToAscii(long number, byte[] buf, int offset, int length) {
        return LongToString(number, buf, offset, length, false);
    }/* ww  w  . j ava2s  .c  o m*/

    public static byte[] LongToAscii(long number, int bufferSize) {
        return LongToString(number, false, bufferSize);
    }

    public static byte[] LongToAscii(long number) {
        return LongToString(number, false, -1);
    }

    public static int LongToString(long number, byte[] buf, int offset, int length, boolean bcdFlag) {
        if (buf == null || offset < 0 || length < 1 || buf.length < (offset + length))
            return -1;
        length = offset + length;
        int charPos = length - 1;
        int numberLength = charPos;
        boolean numberLengthFlag = true;
        int index = 0;
        int radix = (bcdFlag ? 100 : 10);

        if (number > 0) {
            number = -number;
        }
        for (; charPos >= offset; charPos--) {
            if (0 != number) {
                index = (int) (-(number % radix));
                number = number / radix;
            } else {
                index = 0;
            }
            if (numberLengthFlag && 0 == number) {
                numberLengthFlag = !numberLengthFlag;
                numberLength = length - charPos;
            }
            if (bcdFlag) {
                buf[charPos] = BCD_Digit[index];
            } else {
                buf[charPos] = (byte) digits[index];
            }
        }

        return numberLength;
    }

    public static byte[] LongToString(long number, boolean bcdFlag, int bufferSize) {
        int size = (bufferSize > 0 ? bufferSize : 20);
        byte[] buffer = new byte[size];
        int length = LongToString(number, buffer, 0, size, bcdFlag);
        if (bufferSize > 0)
            return buffer;
        byte[] buf = new byte[length];
        System.arraycopy(buffer, size - length, buf, 0, length);
        buffer = null;
        return buf;
    }
}

Related

  1. long2MacAddress(long l)
  2. long2minLeb(final long x)
  3. longTo36Str(String str)
  4. longTo4ByteArray(byte[] bytes, int offset, long value)
  5. longTo4LengthBytes(long num)
  6. longToBaseCode(char[] target, int targetOffset, long value, int base, int lengthLimit, boolean fillZeros, boolean upperCase)
  7. longToBasicType(long l, Class clazz)
  8. longToBcd(long num, int size)
  9. longToBcd(long src, int len, int flag)