Java Integer to Byte Array IntegerToBytes(int i, byte[] dst, int offset)

Here you can find the source of IntegerToBytes(int i, byte[] dst, int offset)

Description

Integer To Bytes

License

Open Source License

Declaration

public static int IntegerToBytes(int i, byte[] dst, int offset) 

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 char[] DigitTens = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1',
            '1', '1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3',
            '3', '3', '3', '3', '3', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5',
            '5', '5', '5', '5', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '7', '7', '7', '7', '7', '7', '7',
            '7', '7', '7', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '9', '9', '9', '9', '9', '9', '9', '9',
            '9', '9', };
    public final static char[] DigitOnes = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3',
            '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4',
            '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', };

    public static int IntegerToBytes(int i, byte[] dst, int offset) {
        return IntegerToBytes(i, dst, offset, true, true, false);
    }/*from   w ww .  j  a v a2  s.  c om*/

    public static int IntegerToBytes(int i, byte[] dst, int offset, boolean minusFlag, boolean lengthFlag,
            boolean getLengthFlag) {
        if ((false == getLengthFlag) && (dst == null || dst.length < offset || dst.length < 1 || offset < 0))
            return -1;

        int length;
        byte[] buf;
        int charPos = 12;

        if (Integer.MIN_VALUE == i) {
            if (minusFlag)
                buf = "-2147483648".getBytes();
            else
                buf = "2147483648".getBytes();

            length = buf.length;
            charPos = 0;
        } else {
            int q, r;
            buf = new byte[12];
            char sign = 0;

            if (i < 0) {
                sign = '-';
                i = -i;
            }

            // Generate two digits per iteration
            while (i >= 65536) {
                q = i / 100;
                // really: r = i - (q * 100);
                r = i - ((q << 6) + (q << 5) + (q << 2));
                i = q;
                buf[--charPos] = (byte) DigitOnes[r];
                buf[--charPos] = (byte) DigitTens[r];
            }

            // Fall thru to fast mode for smaller numbers
            // assert(i <= 65536, i);
            for (;;) {
                q = (i * 52429) >>> (16 + 3);
                r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
                buf[--charPos] = (byte) digits[r];
                i = q;
                if (0 == i)
                    break;
            }
            if (sign != 0 && minusFlag) {
                buf[--charPos] = (byte) sign;
            }
            length = 12 - charPos;
        }

        if (false == getLengthFlag) {
            if (dst != null && dst.length >= offset + length + (lengthFlag ? 1 : 0)) {
                if (lengthFlag) {
                    dst[offset] = (byte) length;
                    offset++;
                }
                System.arraycopy(buf, charPos, dst, offset, length);
                length = offset + length;
            } else {
                length = -1;
            }
        } else {
            if (lengthFlag)
                length = length + 1;
        }

        buf = null;
        return length;
    }
}

Related

  1. int32ToByteArray(int integer)
  2. int32ToByteArray(int value)
  3. integerToByteArray(final int integerToSerialize, final byte[] byteArray, final int offset)
  4. integerToByteArray(int a, byte[] buf)
  5. integerToByteArray(int value)
  6. integerToBytes(int v, byte[] b)
  7. integerToBytes(int v, byte[] b)
  8. integerToBytes(int value)
  9. integerToBytes(int x)