Java Byte Array Create toBytes(int value)

Here you can find the source of toBytes(int value)

Description

to Bytes

License

Open Source License

Declaration

public static byte[] toBytes(int value) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 Shuichi Miura.// www  . j av  a  2  s. co  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Shuichi Miura - initial API and implementation
 ******************************************************************************/

public class Main {
    public static byte[] toBytes(int value) {

        byte[] bs = new byte[4];
        bs[3] = (byte) (0x000000ff & (value));
        bs[2] = (byte) (0x000000ff & (value >>> 8));
        bs[1] = (byte) (0x000000ff & (value >>> 16));
        bs[0] = (byte) (0x000000ff & (value >>> 24));
        return bs;
    }

    public static byte[] toBytes(long value) {

        byte[] bs = new byte[8];
        bs[7] = (byte) (0x000000ff & (value));
        bs[6] = (byte) (0x000000ff & (value >>> 8));
        bs[5] = (byte) (0x000000ff & (value >>> 16));
        bs[4] = (byte) (0x000000ff & (value >>> 24));
        bs[3] = (byte) (0x000000ff & (value >>> 32));
        bs[2] = (byte) (0x000000ff & (value >>> 40));
        bs[1] = (byte) (0x000000ff & (value >>> 48));
        bs[0] = (byte) (0x000000ff & (value >>> 56));
        return bs;
    }

    public static byte[] toBytes(float value) {

        int bits = Float.floatToIntBits(value);
        return toBytes(bits);
    }

    public static byte[] toBytes(double value) {

        long bits = Double.doubleToLongBits(value);
        return toBytes(bits);
    }
}

Related

  1. toBytes(int n)
  2. toBytes(int val)
  3. toBytes(int value)
  4. toBytes(int value)
  5. toBytes(int value)
  6. toBytes(int value)
  7. toBytes(int value, byte[] dest, int destPos)
  8. toBytes(int x, byte[] out)
  9. toBytes(int[] ints)