Java Byte Array Create toByteArray(int in)

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

Description

to Byte Array

License

Open Source License

Declaration

public static byte[] toByteArray(int in) 

Method Source Code

//package com.java2s;
/*// w  w w.j  av  a  2  s .c om
 *
 *  * Copyright (c) 2014- MHISoft LLC and/or its affiliates. All rights reserved.
 *  * Licensed to MHISoft LLC under one or more contributor
 *  * license agreements. See the NOTICE file distributed with
 *  * this work for additional information regarding copyright
 *  * ownership. MHISoft LLC licenses this file to you 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 {
    public static byte[] toByteArray(int in) {
        byte[] out = new byte[4];

        out[0] = (byte) in;
        out[1] = (byte) (in >> 8);
        out[2] = (byte) (in >> 16);
        out[3] = (byte) (in >> 24);

        return out;
    }

    public static byte[] toByteArray(int in, int outSize) {
        byte[] out = new byte[outSize];
        byte[] intArray = toByteArray(in);
        for (int i = 0; i < intArray.length && i < outSize; i++) {
            out[i] = intArray[i];
        }
        return out;
    }
}

Related

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