ByteUtils.java 7.33 KB
package com.adroplat.fist_switch.utils;

import java.nio.ByteBuffer;
import java.util.Arrays;

//import com.adroplat.fist_switch.utils.protocol.remote.RemoteHead;

/**
 * byte转换工具类
 * Created by WLJ on 2015/10/10.
 */
public class ByteUtils {


    /**
     * byte数组转int
     *
     * @param byteArray
     * @return
     */
    public static int byteArrayToInt(byte[] byteArray) {
        int value = 0;
        if (null == byteArray || 4 != byteArray.length) {

        } else {
            int len = byteArray.length;
            for (int i = 0; i < len; i++) {
                value += (byteArray[i] & 0xff) << (8 * (3 - i));
            }
        }
        return value;
    }

    /**
     * int转byte数组
     *
     * @param value
     * @return
     */
    public static byte[] intToByteArrays(int value) {
        byte[] byteArray = new byte[4];
        int len = byteArray.length;
        for (int i = 0; i < len; i++) {
            byteArray[i] = (byte) ((value >> (8 * (3 - i))) & 0xff);
        }
        return byteArray;
    }

    public static String intToIp(int ipInt) {
        StringBuilder builder = new StringBuilder();
        builder.delete(0, builder.length());
        char c = '.';
        return builder.append(((ipInt >> 24) & 0xff)).append(c)
                .append((ipInt >> 16) & 0xff).append(c)
                .append((ipInt >> 8) & 0xff).append(c)
                .append((ipInt & 0xff))
                .toString();
    }

    public static byte[] intToLend(int value) {
        byte[] byteArray = new byte[4];
        int len = byteArray.length;
        for (int i = 0; i < len; i++) {
            byteArray[i] = (byte) ((value >> (8 * i)) & 0xff);
        }
        return byteArray;
    }

    /**
     * byte数组转long
     *
     * @param macArray
     * @return
     */
    public static long macArrayToLong(byte[] macArray) {
        long value = 0;
        if (null == macArray || 8 < macArray.length) {

        } else {
            int len = macArray.length;
            for (int i = 0; i < len; i++) {
                value |= (long) (macArray[i] & 0xff) << (8 * (len - i - 1));
            }
        }
        return value;
    }

    /**
     * long转byte数组
     *
     * @param value
     * @return
     */
    public static byte[] longToMacArray(long value) {
        byte[] lAray = longToBytes(value);
        return Arrays.copyOfRange(lAray, 2, lAray.length);
    }


    /**
     * long转16进制String
     *
     * @param value
     * @return
     */
    public static String longToHexString(long value) {
        byte[] array = longToMacArray(value);
        int offset = 0;
        int len = array.length;
        for (int i = 0; i < len; i++) {
            if (0 != array[i]) {
                offset = i;
                break;
            }
        }
        array = Arrays.copyOfRange(array, offset, len);
        return byteArayToHexString(array).trim();
    }

    /**
     * 16进制String转long
     *
     * @param hex
     * @return
     */
    public static long hexStringToLong(String hex) {
        byte[] array = hexStringToByteAray(hex);
        return macArrayToLong(array);
    }

    /**
     * byte数组转16进制String
     *
     * @param array
     * @return
     */
    public static String byteArayToHexString(byte[] array) {
        String strHex = null;
        if (null == array || 0 == array.length) {
        } else {
            StringBuilder builder = new StringBuilder();
            for (byte b : array) {
                String str = Integer.toHexString(b & 0xff);
                if (1 == str.length()) {
                    str = '0' + str;
                }
                builder.append(str);
            }
            strHex = builder.toString();
        }
        return strHex;
    }

    /**
     * byte数组转16进制MAC
     *
     * @param array
     * @return
     */
    public static String byteArayToMacStr(byte[] array) {
        String strHex = null;
        if (null == array || 0 == array.length) {
        } else {
            StringBuilder builder = new StringBuilder();
            for (byte b : array) {
                String str = Integer.toHexString(b & 0xff);
                if (1 == str.length()) {
                    str = '0' + str;
                }
                builder.append(str + ":");
            }
            strHex = builder.deleteCharAt(builder.length() - 1).toString().toUpperCase();
        }
        return strHex;
    }

    /**
     * 16进制String转byte数组
     *
     * @param hex
     * @return
     */
    public static byte[] hexStringToByteAray(String hex) {
        if (hex == null || hex.equals("")) {
            return null;
        }
        hex = hex.toUpperCase();
        char[] hexChars = hex.toCharArray();
        int len = hexChars.length / 2;
        byte[] d = new byte[len];
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     * Convert char to byte
     *
     * @param c char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /**
     * int转char
     *
     * @param value
     * @return
     */
    public static char intToChar(int value) {
        byte[] byteArray = intToByteArrays(value);
//        return (char) ((byteArray[2] & 0xff) << 8 | byteArray[3] & 0xff);
        return bytesToChar(byteArray);
    }


    /**
     * char转byte[]
     */
    public static byte[] charToBytes(char c) {
        byte[] bytes = new byte[2];
//        final char DIGIT = RemoteHead.DIGIT;
//        bytes[0] = (byte) ((c >> 8) & DIGIT);
//        bytes[1] = (byte) (c & DIGIT);
        return bytes;
    }

    /**
     * byte[]转char
     */
    public static char bytesToChar(byte[] bytes) {
        char c = 0;
        if (null != bytes) {
            int size = bytes.length;
            if (size >= 2) {
                c = (char) ((bytes[size - 2] & 0xff) << 8 | bytes[size - 1] & 0xff);
            } else if (1 == size) {
                c = (char) (bytes[0]);
            }
        }
        return c;
    }

    /**
     * 转化CRON需要的星期
     *
     * @param weekCode
     * @return
     */
    public static String byteToDayWeek(byte weekCode, String weekDay[]) {
        StringBuilder sb = new StringBuilder();
        if (null != weekDay) {
            final int size = weekDay.length;
            for (int i = 0; i < size; i++) {
                if (0 != (weekCode >> i) % 2) {
                    sb.append(weekDay[i]).append(",");
                }
            }
            int len = sb.length();
            if (len > 0) {
                sb.deleteCharAt(len - 1);
            }
        }
        return sb.toString();
    }

    public static byte[] longToBytes(long value) {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putLong(0, value);
        return buffer.array();
    }

    public static long bytesToLong(byte[] value) {
        long result = 0;
        if (null != value) {
            for (int i = 0; i < value.length; i++) {
                value[i] &= 0xff;
            }
            ByteBuffer buffer = ByteBuffer.allocate(8);
            buffer.put(value, 0, value.length);
            buffer.flip();//need flip
            result = buffer.getLong();
        }
        return result;
    }

}