Java Byte Array Create toByteArray(int value, int numBytes, byte[] dest, int off)

Here you can find the source of toByteArray(int value, int numBytes, byte[] dest, int off)

Description

Converts a signed int into an array of bytes and writes it to a byte array t the given offset.

License

Open Source License

Parameter

Parameter Description
value The integral value to byte-ify.
numBytes The number of bytes desired to describe this value (up to 4)
dest The byte array to which to write said bytes.
off The offset index within said byte array from which to start writing said bytes.

Declaration

public static final void toByteArray(int value, int numBytes, byte[] dest, int off) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * SimpleWAVIO - A straightforward library for loading and saving .WAV (RIFF) files as numerical arrays.
 * Copyright (C) 2012,2013  Chuck Ritola
 * //from   w w  w  .j  a va 2  s  .c  om
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

public class Main {
    /**
     * Converts a signed int into an array of bytes and writes it to a byte array t the given offset.
     * @param value      The integral value to byte-ify.
     * @param numBytes   The number of bytes desired to describe this value (up to 4)
     * @param dest      The byte array to which to write said bytes.
     * @param off      The offset index within said byte array from which to start writing said bytes.
     * @since Jul 14, 2012
     */
    public static final void toByteArray(int value, int numBytes, byte[] dest, int off) {
        for (int i = 0; i < numBytes; i++) {
            dest[i + off] = (byte) (value >>> ((i + 2) * 8) & 0xff);
        }
    }
}

Related

  1. toByteArray(int value)
  2. toByteArray(int value)
  3. toByteArray(int value)
  4. toByteArray(int value)
  5. toByteArray(int value)
  6. toByteArray(int[] array)
  7. toByteArray(int[] data)
  8. toByteArray(int[] data, boolean includeLength)
  9. toByteArray(long hi, long lo)