Java Integer to Byte Array int2bytes(int val, byte[] bytes, int offset, boolean bigEndian)

Here you can find the source of int2bytes(int val, byte[] bytes, int offset, boolean bigEndian)

Description

intbytes

License

Apache License

Declaration

public static void int2bytes(int val, byte[] bytes, int offset, boolean bigEndian) 

Method Source Code

//package com.java2s;
/**// w  w w.jav a2  s. c om
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF 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[] int2bytes(int val, boolean bigEndian) {
        byte[] bytes = new byte[4];

        int2bytes(val, bytes, 0, bigEndian);

        return bytes;
    }

    public static void int2bytes(int val, byte[] bytes, int offset, boolean bigEndian) {
        if (bigEndian) {
            bytes[offset + 0] = (byte) ((val >> 24) & 0xff);
            bytes[offset + 1] = (byte) ((val >> 16) & 0xff);
            bytes[offset + 2] = (byte) ((val >> 8) & 0xff);
            bytes[offset + 3] = (byte) ((val) & 0xff);
        } else {
            bytes[offset + 3] = (byte) ((val >> 24) & 0xff);
            bytes[offset + 2] = (byte) ((val >> 16) & 0xff);
            bytes[offset + 1] = (byte) ((val >> 8) & 0xff);
            bytes[offset + 0] = (byte) ((val) & 0xff);
        }
    }
}

Related

  1. int2bytes(int num)
  2. int2Bytes(int num)
  3. int2bytes(int num)
  4. int2bytes(int num, int len)
  5. int2bytes(int v)
  6. int2bytes(int value)
  7. int2bytes(int value)
  8. int2Bytes(int value)
  9. int2bytes(int value, byte[] bytes, int off)