Java Byte Array Copy copyBytes(byte[] dst, int dstPos, int value)

Here you can find the source of copyBytes(byte[] dst, int dstPos, int value)

Description

copy Bytes

License

Open Source License

Declaration

public static void copyBytes(byte[] dst, int dstPos, int value) 

Method Source Code

//package com.java2s;

public class Main {
    public static void copyBytes(byte[] dst, int dstPos, int value) {
        byte[] valueBytes = toByteArray(value);

        System.arraycopy(valueBytes, 0, dst, dstPos, valueBytes.length);
    }/*w  ww. j  a  va2  s .c  o  m*/

    public static void copyBytes(byte[] dst, int dstPos, short value) {
        byte[] valueBytes = toByteArray(value);
        System.arraycopy(valueBytes, 0, dst, dstPos, valueBytes.length);
    }

    public static void copyBytes(byte[] dst, int dstPos, byte value) {
        dst[dstPos] = value;
    }

    public static void copyBytes(byte[] dst, int dstPos, String value) {
        byte[] valueBytes = value.getBytes();
        System.arraycopy(valueBytes, 0, dst, dstPos, valueBytes.length);
    }

    public static byte[] toByteArray(long value) {
        byte[] b = new byte[8];
        for (int i = 0; i < 8; i++) {
            int offset = (8 - 1 - i) * 8;
            b[i] = (byte) (value >>> offset & 0xFF);
        }
        return b;
    }

    public static byte[] toByteArray(int value) {
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            int offset = (4 - 1 - i) * 8;
            b[i] = (byte) (value >>> offset & 0xFF);
        }
        return b;
    }

    public static byte[] toByteArray(short value) {
        byte[] b = new byte[2];
        for (int i = 0; i < 2; i++) {
            int offset = (2 - 1 - i) * 8;
            b[i] = (byte) (value >>> offset & 0xFF);
        }
        return b;
    }
}

Related

  1. copyByte(byte[] ASource, int nFrom, int nEnd)
  2. copyByteArray(byte[] array2Copy)
  3. copyByteArray(byte[] source)
  4. copyByteArray(final byte[] src, byte[] dest, int length)
  5. copyBytes(byte[] arr, int length)
  6. copyBytes(byte[] dstBytes, int dstFrom, byte[] srcBytes, int srcFrom, int len)
  7. copyBytes(byte[] from, byte[] to, int fromIndex)
  8. copyBytes(byte[] from, int offset, int len)
  9. copyBytes(byte[] results, int offset, int int32)