package com.desfate.chart.util;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * Created by cxl on 2018/3/14.
 */

public class ByteUtils {
    public static int bytesToInt(byte[] datas, int index, ByteOrder orderType) {
        ByteBuffer buffer = ByteBuffer.wrap(datas, index, 4);
        buffer.order(orderType);
        return buffer.getInt();
    }

    public static long bytesToLong(byte[] datas, int index, ByteOrder orderType) {
        ByteBuffer buffer = ByteBuffer.wrap(datas, index, 8);
        buffer.order(orderType);
        return buffer.getLong();
    }

    public static byte[] doubleToBytes(double value, ByteOrder orderType) {
        return ByteBuffer.allocate(8).order(orderType).putDouble(value).array();
    }

    public static byte[] longToBytes(long value, ByteOrder orderType) {
        return ByteBuffer.allocate(8).order(orderType).putLong(value).array();
    }


    public static byte[] intToBytes(int value, ByteOrder orderType) {
        return ByteBuffer.allocate(4).order(orderType).putInt(value).array();
    }

    public static byte[] intToBytes(int value, ByteOrder orderType, boolean type) {
        return ByteBuffer.allocate(8).order(orderType).putInt(value).array();
    }

    public static double bytesToDouble(byte[] datas, int index, ByteOrder orderType) {
        ByteBuffer buffer = ByteBuffer.wrap(datas, index, 8);
        buffer.order(orderType);
        return buffer.getDouble();
    }

    public static short bytesToShort(byte[] datas, int index, ByteOrder orderType) {
        ByteBuffer buffer = ByteBuffer.wrap(datas, index, 2);
        buffer.order(orderType);
        return buffer.getShort();
    }

    public static byte[] shortToBytes(short value, ByteOrder orderType) {
        return ByteBuffer.allocate(2).order(orderType).putShort(value).array();
    }

    /**
     * 将string转换为byte
     *
     * @param string
     * @return
     */
    public static byte[] stringToHex(String string) {
        if (string.length() % 2 != 0) {// 不行
            return null;
        }
        byte[] bs = new byte[string.length() / 2];
        char[] cs = string.toCharArray();
        int height = 0;
        int low = 0;
        for (int i = 0; i < cs.length; i += 2) {//
            height = getInt(cs[i]);
            low = getInt(cs[i + 1]);

            if (low == 999 || height == 999) {
                return null;
            }

            int byteValue = height * 16 + low;
            bs[i / 2] = (byte) byteValue;
        }
        System.gc();
        return bs;
    }

    /**
     * 将byte转换为string
     *
     * @param bytes
     * @return
     */
    public static String TSbyteToString(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            int b = (bytes[i] & 0xFF);
            String dd = Integer.toHexString(b);
            if (dd.length() < 2) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(b));
        }
        return sb.toString();
    }


    private static int getInt(char c) {
        if (c >= '0' && c <= '9') {// 转化为10进制
            return c - '0';
        } else if (c >= 'A' && c <= 'Z') {
            return c - 'A' + 10;
        } else if (c >= 'a' && c <= 'z') {
            return c - 'a' + 10;
        }
        return 999;
    }
}
