Java Integer to Byte Array integerToBytes(int v, byte[] b)

Here you can find the source of integerToBytes(int v, byte[] b)

Description

integer To Bytes

License

Open Source License

Declaration

public final static void integerToBytes(int v, byte[] b) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004,2009 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  w  w w . j a v  a 2 s .  c  o  m*/
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

public class Main {
    public final static void integerToBytes(int v, byte[] b) {
        assert b.length >= 4;
        b[0] = (byte) ((v >>> 24) & 0xFF);
        b[1] = (byte) ((v >>> 16) & 0xFF);
        b[2] = (byte) ((v >>> 8) & 0xFF);
        b[3] = (byte) ((v >>> 0) & 0xFF);
    }

    public final static void integerToBytes(int v, byte[] b, int off) {
        assert b.length - off >= 4;
        b[off++] = (byte) ((v >>> 24) & 0xFF);
        b[off++] = (byte) ((v >>> 16) & 0xFF);
        b[off++] = (byte) ((v >>> 8) & 0xFF);
        b[off] = (byte) ((v >>> 0) & 0xFF);
    }
}

Related

  1. int32ToByteArray(int value)
  2. integerToByteArray(final int integerToSerialize, final byte[] byteArray, final int offset)
  3. integerToByteArray(int a, byte[] buf)
  4. integerToByteArray(int value)
  5. IntegerToBytes(int i, byte[] dst, int offset)
  6. integerToBytes(int v, byte[] b)
  7. integerToBytes(int value)
  8. integerToBytes(int x)
  9. intTo2Bytes(int x)