Java Byte Array Create toByteArray(int intr)

Here you can find the source of toByteArray(int intr)

Description

This method converts one integer number to little-endian byte array.

License

Open Source License

Parameter

Parameter Description
intr the number which type of integer

Declaration

public static byte[] toByteArray(int intr) 

Method Source Code

//package com.java2s;
/*//from   w  ww  . j  a  va2 s. c  om
 * This file is part of GenoViewer.
 *
 * GenoViewer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GenoViewer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GenoViewer.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * 8-bit mask for performing operations byte by byte.
     */
    private static final int OP_PATTERN = 0xFF;

    /**
     * This method converts one integer number to little-endian byte array.
     *
     * @param intr the number which type of integer
     */
    public static byte[] toByteArray(int intr) {
        byte[] result = new byte[4];

        for (int i = 0; i < 4; ++i) {
            result[i] = (byte) (intr & OP_PATTERN);
            intr = (intr >>> 8);
        }

        return result;
    }

    /**
     * This method converts one short number to little-endian byte array.
     *
     * @param shrt the number which type of short
     */
    public static byte[] toByteArray(short shrt) {
        byte[] result = new byte[2];
        int intr = shrt;

        for (int i = 0; i < 2; ++i) {
            result[i] = (byte) (intr & OP_PATTERN);
            intr = (intr >>> 8);
        }

        return result;
    }

    /**
     * Converts a long value to little-endian byte array.
     *
     * @param value the long value to be converted
     */
    public static byte[] toByteArray(long value) {
        byte[] result = new byte[8];

        for (int i = 0; i < 8; ++i) {
            result[i] = (byte) (value & OP_PATTERN);
            value = (value >>> 8);
        }

        return result;
    }

    /**
     * This method converts one character array to byte array.
     *
     * @param charArray characters contain array
     */
    public static byte[] toByteArray(char[] charArray) {
        byte[] result = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++) {
            result[i] = (byte) charArray[i];
        }

        return result;
    }
}

Related

  1. toByteArray(final String hexString)
  2. toByteArray(int data)
  3. toByteArray(int i)
  4. toByteArray(int in)
  5. toByteArray(int in, int size)
  6. toByteArray(int num)
  7. toByteArray(int value)
  8. toByteArray(int value)
  9. toByteArray(int value)