Java Convert via ByteBuffer 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;
import java.nio.ByteBuffer;

public class Main {
    public static byte[] toBytes(int value) {
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putInt(value).flip();//w  w  w.  j a va  2  s .c om

        return readBytes(buffer, 4);
    }

    public static byte[] readBytes(ByteBuffer buffer, int length) {
        if (buffer == null || length <= 0) {
            return null;
        }

        byte[] bytes = new byte[length];
        buffer.get(bytes);

        return bytes;
    }
}

Related

  1. toBytes(char[] chars)
  2. toBytes(char[] string)
  3. toBytes(final float val)
  4. toBytes(final UUID uuid)
  5. toBytes(InputStream input)
  6. toBytes(long l)
  7. toBytes(Number value)
  8. toBytes(Object obj)
  9. toBytes(String str, boolean wideChar)