Java Integer to Byte Array integerToByteArray(final int integerToSerialize, final byte[] byteArray, final int offset)

Here you can find the source of integerToByteArray(final int integerToSerialize, final byte[] byteArray, final int offset)

Description

Serializes and writes the given integer number to the provided byte array.

License

Apache License

Parameter

Parameter Description
integerToSerialize the integer number of serialize
byteArray the byte array to write to
offset the offset at which to start writing inside the byte array

Declaration

public static void integerToByteArray(final int integerToSerialize, final byte[] byteArray, final int offset) 

Method Source Code

//package com.java2s;
/***********************************************************************************************************************
 *
 * Copyright (C) 2010-2013 by the Stratosphere project (http://stratosphere.eu)
 *
 * 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 {
    /**//w  w  w  .jav a 2 s  .  co  m
     * Serializes and writes the given integer number to the provided byte array.
     * 
     * @param integerToSerialize
     *        the integer number of serialize
     * @param byteArray
     *        the byte array to write to
     * @param offset
     *        the offset at which to start writing inside the byte array
     */
    public static void integerToByteArray(final int integerToSerialize, final byte[] byteArray, final int offset) {

        for (int i = 0; i < 4; ++i) {
            final int shift = i << 3; // i * 8
            byteArray[offset + 3 - i] = (byte) ((integerToSerialize & 0xff << shift) >>> shift);
        }
    }
}

Related

  1. int32ToByteArr(int val, byte[] arr, int offset)
  2. int32ToByteArray(int a)
  3. int32toByteArray(int i)
  4. int32ToByteArray(int integer)
  5. int32ToByteArray(int value)
  6. integerToByteArray(int a, byte[] buf)
  7. integerToByteArray(int value)
  8. IntegerToBytes(int i, byte[] dst, int offset)
  9. integerToBytes(int v, byte[] b)