Java Integer to Byte Array intToByteArray(int value, byte[] buffer, int offset)

Here you can find the source of intToByteArray(int value, byte[] buffer, int offset)

Description

Convert the provided into into a byte[] and write the values into the given buffer starting at the provided offset (this will take up 4 bytes!)

License

Open Source License

Declaration

public static void intToByteArray(int value, byte[] buffer, int offset) 

Method Source Code

//package com.java2s;
/*/*  w  w  w. j a v  a 2  s. c  o m*/
 *   Copyright 2008 The Portico Project
 *
 *   This file is part of portico.
 *
 *   portico is free software; you can redistribute it and/or modify
 *   it under the terms of the Common Developer and Distribution License (CDDL) 
 *   as published by Sun Microsystems. For more information see the LICENSE file.
 *   
 *   Use of this software is strictly AT YOUR OWN RISK!!!
 *   If something bad happens you do not have permission to come crying to me.
 *   (that goes for your lawyer as well)
 *
 */

public class Main {
    /**
     * Convert the provided int into a byte[] for transmission over the network.
     */
    public static byte[] intToByteArray(int value) {
        return new byte[] { (byte) (value >>> 24), (byte) (value >>> 16), (byte) (value >>> 8), (byte) value };
    }

    /**
     * Convert the provided into into a byte[] and write the values into the given buffer starting
     * at the provided offset (this will take up 4 bytes!)
     */
    public static void intToByteArray(int value, byte[] buffer, int offset) {
        buffer[offset] = (byte) (value >>> 24);
        buffer[offset + 1] = (byte) (value >>> 16);
        buffer[offset + 2] = (byte) (value >>> 8);
        buffer[offset + 3] = (byte) (value);
    }
}

Related

  1. intToByteArray(int value)
  2. intToByteArray(int value)
  3. intToByteArray(int value)
  4. intToByteArray(int value)
  5. intToByteArray(int value)
  6. intToByteArray(int value, byte[] data, int offset)
  7. intToByteArray(int value, byte[] dest)
  8. intToByteArray(int value, int nrOfBytes)
  9. intToByteArray(int value, int numBytes)