Here you can find the source of integerToByteArray(final int integerToSerialize, final byte[] byteArray, final int offset)
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 |
public static void integerToByteArray(final int integerToSerialize, final byte[] byteArray, final int offset)
//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); } } }