LabelClient.java 4.71 KB
package com.huaheng.api.general.controller;


import java.io.IOException;
import java.net.Socket;

/**
 * 客户端
 * @author lty
 */
public class LabelClient {


    /**
     * 十六进制字符串转字节数组
     *
     * @param src
     * @return
     */
    public static byte[] hexString2Bytes(String src) {
        int l = src.length() / 2;
        byte[] ret = new byte[l];
        for (int i = 0; i < l; i++) {
            ret[i] = Integer
                    .valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
        }
        return ret;
    }

    /**
     * decription  发送并接收指令响应
     *
     * @author lty
     * @date 2022/7/18 17:05
     */
    public static String sendAndReceive(String cmd, Socket socket) {
        //socket连接建立
        String res = null;
        try {
            sendCmd(cmd, socket);
        } catch (Exception e) {
        }
        return res;

    }

    /**
     * 字符串转unicode
     *
     * @param str 字符串
     * @return unicode
     */
    public static String stringToUnicode(String str) {
        StringBuffer sb = new StringBuffer();
        String val = null;
        char[] c = str.toCharArray();
        for (int i = 0; i < c.length; i++) {
            // Integer.toHexString把字符串转16进制
            val = Integer.toHexString(c[i]);
            System.out.println(val.length());


            if (val.length() == 4) {
                String s = String.valueOf(val);
                char ss[]=s.toCharArray();
                val = String.valueOf(ss[2])+String.valueOf(ss[3])+String.valueOf(ss[0])+String.valueOf(ss[1]);
            }


            if (val.length() == 1) {
                val = "0" + val;
            }
            if (val.length() == 2) {
                val = val + "00";
            }

            sb.append(val);
        }
        return sb.toString();
    }

    // 16进制直接转换成为字符串(无需Unicode解码)
    public static String hexStr2Str(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2]; //1个byte数值 -> 两个16进制字符
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            // 保持二进制补码的一致性 因为byte类型字符是8bit的  而int为32bit 会自动补齐高位1  所以与上0xFF之后可以保持高位一致性
            //当byte要转化为int的时候,高的24位必然会补1,这样,其二进制补码其实已经不一致了,&0xff可以将高的24位置为0,低8位保持原样,这样做的目的就是为了保证二进制数据的一致性。
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
        // return new String(bytes,"gb2312"); 转换时指定,则解析时指定


    }

    // 字符串转换成为16进制(无需Unicode编码)
    public static String str2HexStr(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder(); //String的getBytes()方法是得到一个操作系统默认的编码格式的字节数组
        byte[] bs = str.getBytes();

        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4; // 高4位, 与操作 1111 0000
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f; // 低四位, 与操作 0000 1111
            sb.append(chars[bit]);
        }
        return sb.toString().trim();
    }

    /**
     * decription 发送指令
     *
     * @author wangjingjing
     * @date 2020/9/29 17:18
     */
    public static void sendCmd(String cmd, Socket socket) {
        try {
            socket.getOutputStream().write(hexString2Bytes(cmd));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public Socket init() throws Exception {

        Socket socket = new Socket("127.0.0.1", 60000);
        socket.setKeepAlive(true);
        //超时时间2秒
        socket.setSoTimeout(2000);
        return socket;
    }

//    public static String readRes(Socket socket) {
//        // 从服务端程序接收数据
//        String result = "";
//        InputStream in = null;
//        try {
//            in = socket.getInputStream();
////            while (in.available() != 0) {
////                byte b = (byte) in.read();
////                String hex = "";
////                hex = String.valueOf(HEXSTR.charAt((b & 0xF0) >> 4));
////                hex += String.valueOf(HEXSTR.charAt(b & 0x0F));
////                result += hex; }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return result;
//    }

}