TCP用到的轉換函數

接上篇TCP

本文主要是TCP連接中所用到的一些轉換函數:

public class DataUtil {

//buffer轉string
    public static String decodeKey(ByteBuffer bytes) {
        Charset charset = Charset.forName("utf-8");
        return charset.decode(bytes).toString();
    }

//buffer轉byte[]
    public static byte[] decodeValue(ByteBuffer bytes) {

        int len = bytes.limit() - bytes.position();
        byte[] bytes1 = new byte[bytes.limit()];
        if (bytes.hasArray()){

            System.arraycopy(bytes.array(),0,bytes1,0,bytes.limit());
            return bytes1;
        }
        return null;
    }

//string轉buffer
    public static ByteBuffer encodeKey(String key) {
        try {
            return ByteBuffer.wrap(key.getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return ByteBuffer.wrap(key.getBytes());
    }

//byte[]轉 buffer
    public static ByteBuffer encodeValue(byte[] value) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(value.length);
        byteBuffer.put(value);
        byteBuffer.flip();
        return byteBuffer;
    }

    /**
     * 將字節數組轉換成十六進制的字符串
     *
     * @return
     */
    public static String BinaryToHexString(byte[] bytes) {
        String hexStr = "0123456789ABCDEF";
        String result = "";
        String hex = "";
        for (byte b : bytes) {
            hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
            hex += String.valueOf(hexStr.charAt(b & 0x0F));
            result += hex + " ";
        }
        return result;
    }
    /**
     * 將十六進制的字符串轉換成字節數組
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStrToBinaryStr(String hexString) {

        if (hexString.isEmpty()) {
            return null;
        }
        hexString = hexString.replaceAll(" ", "");
        int len = hexString.length();
        int index = 0;
        byte[] bytes = new byte[len / 2];
        while (index < len) {
            String sub = hexString.substring(index, index + 2);
            bytes[index/2] = (byte)Integer.parseInt(sub,16);
            index += 2;
        }
        return bytes;
    }
    /**
     * 16進制浮點轉10進制
     * @return
     */
    public static double modbusFloat(String s) {
        byte[] bits = hexStrToBinaryStr(s);
        double rtn = 0;
        int flag = 1;
        if (bits.length == 4) {
            int E = bits[0] & 0x80;
            if (E > 0) {
                flag = -1;
            }

            E = ((bits[0] & 0x7F) << 1) + ((bits[1] & 0x80) >> 7);
            double M10 = 0.0;
            for (int i = 1; i <= 7; i++) {
                M10 += (bits[1] >> (7 - i) & 0x01) * Math.pow(2, -1 * i);
            }
            for (int i = 1; i <= 8; i++) {
                M10 += (bits[2] >> (8 - i) & 0x01) * Math.pow(2, -1 * (i + 7));
            }
            for (int i = 1; i <= 8; i++) {
                M10 += (bits[3] >> (8 - i) & 0x01) * Math.pow(2, -1 * (i + 15));
            }

            String data = "" + (1.0 + M10);
            double dVal = Double.parseDouble(data);
            rtn = flag * dVal * (Math.pow(2, E - 127));
        }
        return rtn;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章