Java Byte Array Create byteArrayFromBytes(int... bytes)

Here you can find the source of byteArrayFromBytes(int... bytes)

Description

Return an array of bytes containing as elements this method's parameters.

License

Open Source License

Parameter

Parameter Description
bytes 0 or more bytes to convert into an array of bytes

Return

array of bytes

Declaration

public static byte[] byteArrayFromBytes(int... bytes) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w.j a  v a 2  s . co  m*/
 * Copyright (c) 2012-2015, Microsoft Mobile
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

public class Main {
    /**
     * Return an array of bytes containing as elements this method's parameters.  Parameters are ints but they should be
     * in a legal range for a byte (technically -128 to 127, but treating the range like 0-255 also works as expected).
     * <p/>
     * This method exists mostly to avoid casting problems populating a byte[] literals inline--casting issues caused by
     * integer literals always being ints not bytes in Java (and thus requiring a byte cast--compile issue #1) and
     * translated C# always treating hex literals as unsigned and not allowing ones > 127 to be cast to (signed) bytes
     * (compile issue #2). Anyway, that's all avoided just by passing in ints here.
     *
     * @param bytes 0 or more bytes to convert into an array of bytes
     * @return array of bytes
     */
    public static byte[] byteArrayFromBytes(int... bytes) {
        int length = bytes.length;

        byte[] byteArray = new byte[length];
        for (int i = 0; i < length; i++)
            byteArray[i] = (byte) bytes[i];

        return byteArray;
    }
}

Related

  1. byteArrayFromChar(char i)
  2. byteArrayFromHexString(String in)
  3. byteArrayFromInt(final int number)
  4. byteArrayFromInteger(int integer)