Java ByteOrder toBytes(int value, ByteOrder order)

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

Description

to Bytes

License

Open Source License

Declaration

public static byte[] toBytes(int value, ByteOrder order) 

Method Source Code


//package com.java2s;
import java.nio.ByteOrder;

public class Main {
    public static final int COUNT = 4;

    public static byte[] toBytes(int value, ByteOrder order) {
        byte[] result = new byte[COUNT];
        if (ByteOrder.BIG_ENDIAN.equals(order)) {
            int i = COUNT - 1;
            do {/*from   ww w.  java2 s  .  c o m*/
                result[i] = (byte) (value % 256);
                value = (int) (value / 256);
                --i;
            } while (i >= 0);
        } else {
            int i = 0;
            do {
                result[i] = (byte) (value % 256);
                value = (int) (value / 256);
                ++i;
            } while (i < COUNT);
        }
        return result;
    }
}

Related

  1. isNegative(byte[] bytes, ByteOrder byteOrder, boolean isSigned)
  2. longToBytes(long longValue, ByteOrder byteOrder, boolean isSigned)
  3. opposite(ByteOrder order)
  4. reduceToSmallestByteArray(byte[] originalBytes, ByteOrder byteOrder, boolean isSigned)
  5. setLong(byte[] bytes, int start, int end, long value, ByteOrder byteOrder)
  6. toDoubleByteArray(double val, ByteOrder outOrder, byte[] buf, int off)
  7. toFloatByteArray(float val, ByteOrder outOrder, byte[] buf, int off)
  8. toInt(byte[] bytes, ByteOrder order)
  9. toInt16ByteArray(final int val, final ByteOrder outOrder, final byte[] buf, final int off)