Java Integer Array to Byte Array convertIntArrayToByteArray(int[] data)

Here you can find the source of convertIntArrayToByteArray(int[] data)

Description

Convert int array into byte array

License

Apache License

Declaration

public static byte[] convertIntArrayToByteArray(int[] data) 

Method Source Code

//package com.java2s;
/*// w  w  w  .  j a  v a 2s.  c  o  m
 * Copyright 2004 Senunkan Shinryuu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Convert int array into byte array
     */
    public static byte[] convertIntArrayToByteArray(int[] data) {
        byte[] dataByte = new byte[data.length];
        for (int i = 0; i < data.length; i++) {
            if (data[i] < 128) {
                dataByte[i] = (byte) data[i];
            } else if (data[i] >= 128 && data[i] < 256) {
                dataByte[i] = (byte) (data[i] - 256);
            } else {
                dataByte[i] = 0;
            }
        }
        return dataByte;
    }
}

Related

  1. convertIntArrayToByteArray(int[] myIntArray, int bytesPerInt)
  2. intArray2Bytes(int[] array)
  3. intArrayToByteArray(int[] ai)
  4. intArrayToByteArray(int[] data)