Java Integer to Byte Array IntToByteArray(int val, byte[] buf, int offset)

Here you can find the source of IntToByteArray(int val, byte[] buf, int offset)

Description

Write integer into buf[offset]...buf[offset+3]

License

Apache License

Parameter

Parameter Description
val Integer value
buf Byte array
offset Offset into the byte array

Declaration

public static void IntToByteArray(int val, byte[] buf, int offset) 

Method Source Code

//package com.java2s;
/*/*from  ww  w.j  av a2s .c  o m*/
 *
 *  Copyright (c) 2015 University of Massachusetts
 *
 *  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.
 *
 *  Initial developer(s): Westy
 *
 */

public class Main {
    /**
     * Write integer into buf[offset]...buf[offset+3]
     *
     * @param val Integer value
     * @param buf Byte array
     * @param offset Offset into the byte array
     */
    public static void IntToByteArray(int val, byte[] buf, int offset) {
        buf[0 + offset] = (byte) ((val >> 24) & 255);
        buf[1 + offset] = (byte) ((val >> 16) & 255);
        buf[2 + offset] = (byte) ((val >> 8) & 255);
        buf[3 + offset] = (byte) (val & 255);
    }

    /**
     * Write integer into buf[0]...buf[3]
     *
     * @param val Integer value
     * @param buf Byte array
     */
    public static void IntToByteArray(int val, byte[] buf) {
        IntToByteArray(val, buf, 0);
    }
}

Related

  1. intToByteArray(int integer)
  2. intToByteArray(int integer)
  3. intToByteArray(int piValueToConvert, boolean pbBigEndian)
  4. intToByteArray(int pSrc, byte[] pDst, int pOffset)
  5. intToByteArray(int source)
  6. intToByteArray(int value)
  7. intToByteArray(int value)
  8. intToByteArray(int value)
  9. intToByteArray(int value)