package com.eactive.eai.transformer.util;

public class ConvertUtil {
	private ConvertUtil() {
		
	}
    public static byte[] int2bytes(int value) {
        byte[] data = new byte[4];
        data[0] = (byte) ((value >> 24) & 0xFF);
        data[1] = (byte) ((value >> 16) & 0xFF);
        data[2] = (byte) ((value >> 8) & 0xFF);
        data[3] = (byte) ((value) & 0xFF);

        return data;
    }

    public static byte[] long2bytes(long value) {
        byte[] data = new byte[8];
        data[0] = (byte) ((value >> 56) & 0xFF);
        data[1] = (byte) ((value >> 48) & 0xFF);
        data[2] = (byte) ((value >> 40) & 0xFF);
        data[3] = (byte) ((value >> 32) & 0xFF);
        data[4] = (byte) ((value >> 24) & 0xFF);
        data[5] = (byte) ((value >> 16) & 0xFF);
        data[6] = (byte) ((value >> 8) & 0xFF);
        data[7] = (byte) ((value) & 0xFF);

        return data;
    }

    public static byte[] short2bytes(short value) {
        byte[] data = new byte[8];
        data[0] = (byte) ((value >> 8) & 0xFF);
        data[1] = (byte) ((value) & 0xFF);

        return data;
    }

    public static int bytes2int(byte[] value, int offset) {
        int v = 0;
        v += (((int) value[offset] << 24) & 0xFF000000);
        v += (((int) value[offset + 1] << 16) & 0x00FF0000);
        v += (((int) value[offset + 2] << 8) & 0x0000FF00);
        v += (((int) value[offset + 3]) & 0x000000FF);

        return v;
    }

    public static long bytes2long(byte[] value, int offset) {
        long v = 0;
        v += (((long) value[offset] << 56) & 0xFF00000000000000L);
        v += (((long) value[offset + 1] << 48) & 0x00FF000000000000L);
        v += (((long) value[offset + 2] << 40) & 0x0000FF0000000000L);
        v += (((long) value[offset + 3] << 32) & 0x000000FF00000000L);
        v += (((long) value[offset + 4] << 24) & 0x00000000FF000000L);
        v += (((long) value[offset + 5] << 16) & 0x0000000000FF0000L);
        v += (((long) value[offset + 6] << 8) & 0x000000000000FF00L);
        v += (((long) value[offset + 7]) & 0x00000000000000FFL);

        return v;
    }

    public static short bytes2short(byte[] value, int offset) {
        short v = 0;
        v += (((short) value[offset] << 8) & 0xFF00);
        v += (((short) value[offset + 1]) & 0x00FF);

        return v;
    }
}
