Java List to Int List toIntArray(List bytes)

Here you can find the source of toIntArray(List bytes)

Description

Coverts a data stream represented as an array of bytes to an array of integers.

License

Open Source License

Parameter

Parameter Description
bytes list of bytes to be converted

Exception

Parameter Description
IllegalArgumentException an exception

Return

list of integers containing the same data stream, MSB first order.

Declaration

public static ArrayList<Integer> toIntArray(List<Byte> bytes)
        throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*//from w ww.  ja v a 2s .c  om
 * Copyright (c) 2010-2011 Brigham Young University
 * 
 * This file is part of the BYU RapidSmith Tools.
 * 
 * BYU RapidSmith Tools is free software: you may redistribute it 
 * and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either version 2 of 
 * the License, or (at your option) any later version.
 * 
 * BYU RapidSmith Tools 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.
 * 
 * A copy of the GNU General Public License is included with the BYU 
 * RapidSmith Tools. It can be found at doc/gpl2.txt. You may also 
 * get a copy of the license at <http://www.gnu.org/licenses/>.
 * 
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Coverts a data stream represented as an array of bytes to an array of
     * integers. Packs bytes in groups of 4 into integers, most significant bit
     * first.
     * 
     * @param bytes list of bytes to be converted
     * @return list of integers containing the same data stream, MSB first order.
     * @throws IllegalArgumentException
     */
    public static ArrayList<Integer> toIntArray(List<Byte> bytes)
            throws IllegalArgumentException {
        if (bytes.size() % 4 != 0) {
            throw new IllegalArgumentException(
                    "Integer Array can only be created from byte arrays where size%4 is 0");
        }
        ArrayList<Integer> integers = new ArrayList<Integer>();
        for (int index = 0; index < bytes.size(); index += 4) {
            integers.add(new Integer(
                    ((bytes.get(index) << 24) & 0xff000000)
                            | ((bytes.get(index + 1) << 16) & 0xff0000)
                            | ((bytes.get(index + 2) << 8) & 0xff00)
                            | (bytes.get(index + 3) & 0xff)));
        }
        return integers;
    }
}

Related

  1. toIntArray(final List list)
  2. toIntArray(java.util.List list)
  3. toIntArray(List list)
  4. toIntArray(List nums)
  5. toIntArray(List ints)
  6. toIntArray(List a)
  7. toIntArray(List integerList)
  8. toIntArray(List intList)
  9. toIntArray(List list)