Java Integer to Byte Array convertIntegerToByteArray(int value, int bytes, boolean revers)

Here you can find the source of convertIntegerToByteArray(int value, int bytes, boolean revers)

Description

convert Integer To Byte Array

License

LGPL

Declaration

public static final byte[] convertIntegerToByteArray(int value,
            int bytes, boolean revers) 

Method Source Code

//package com.java2s;
//    ????????? ?????????? ? GNU Lesser General Public License ??????? 3 ? ????.

public class Main {
    public static final byte[] convertIntegerToByteArray(int value,
            int bytes, boolean revers) {
        if (bytes == 1 && !revers) {
            return new byte[] { (byte) value };
        } else if (bytes == 1 && revers) {
            return new byte[] { (byte) (value >>> 24) };
        } else if (bytes == 2 && !revers) {
            return new byte[] { (byte) (value >>> 8), (byte) value };
        } else if (bytes == 2 && revers) {
            return new byte[] { (byte) (value >>> 24),
                    (byte) (value >>> 16) };
        } else if (bytes == 3 && !revers) {
            return new byte[] { (byte) (value >>> 16),
                    (byte) (value >>> 8), (byte) value };
        } else if (bytes == 3 && revers) {
            return new byte[] { (byte) (value >>> 24),
                    (byte) (value >>> 16), (byte) (value >>> 8) };
        } else {//www.j a  v a2 s  .c o m
            return new byte[] { (byte) (value >>> 24),
                    (byte) (value >>> 16), (byte) (value >>> 8),
                    (byte) value };
        }
    }
}

Related

  1. convertIntegersToBytes(int[] integers)
  2. convertIntToByteArray(int value)
  3. convertIntToByteArray(int value, int numberOfBytes)
  4. convertIntToByteArray(int[] rgb)
  5. convertIntToBytes(int i)