socket 發送接收16進制數據

背景:做了個智能櫃管理系統,需要和智能櫃對接。交互使用的是socket。

轉換邏輯:

發送規則:數據(1)---->16進制(0x01)---->高4位(0x00)、低四位(0x01)---->10進制(0、1)---->ansi碼(48,49)---->16進制(0x30,0x31)---->發送

ansi如圖

發送接收數據:start爲碼頭,end爲碼尾加驗證。是和第三方溝通的標誌。

     /**
     * 發送指令
     * @param ip 智能櫃ip地址
     * @param com 指令類型
     * @param data 數據
     * @return
     */
    public static String sendSocket(String ip,byte[] com,byte[] data){
        try{
            Socket socket = new Socket(ip,port);
            //獲取一個輸出流,向服務端發送信息
            OutputStream outputStream=socket.getOutputStream();
            outputStream.write(start);
            outputStream.write(com);
            if(data!=null){
                outputStream.write(data);
            }
            outputStream.write(end);
            socket.shutdownOutput();
            //獲取一個輸入流,接收服務端的信息
            InputStream inputStream=socket.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(inputStream);
            DataInputStream dis = new DataInputStream(bis);
            byte[] bytes = new byte[1];
            String ret = "";
            byte[] result = new byte[1024];
            int i = 0;
            while (dis.read(bytes) != -1) {
                ret += bytesToHexString(bytes) + " ";
                result[1] = bytes[0];
            }
            System.out.println(ret);
            //關閉相對應的資源
            dis.close();
            inputStream.close();
            outputStream.close();
            socket.close();
            return ret;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 將接收到的數據轉換爲16進制
     * @param bytes
     * @return
     */
    public static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }

步驟:傳遞一個數後獲取高4位height,低4位low。在根據ansi數組獲取值

    /**
     * 0-f  0x30,0x46
     */
    public static byte[] ansi = new byte[]{0x30,0x31,0x32,0x33,0x34,
            0x35,0x36,0x37,0x38,0x39,
            0x41,0x42,0x43,0x44,0x45,0x46};
    /**
     * 將一個integer值轉換成兩個0xXX的字符串。integer要小於255.
     * @param code
     * @return
     */
    public static byte[] transfer(Integer code){
        if(code==null){
            return null;
        }
        int height = ((code & 0xf0) >> 4);
        int low = (code & 0x0f);
        byte[] b = new byte[2];
        b[0] = ansi[height];
        b[1] = ansi[low];
        return b;
    }

接收數據:33 32----> 3,2 ---->0x32 ---->2

    private static final String HexStr = "0123456789abcdef";
    private static Map<String,String> getMap(){
        if(map.isEmpty()){
            for(int i=0;i<16;i++){
                if(i<=9){
                    map.put(i+30+"",HexStr.substring(i,i+1));
                }else{
                    map.put(i+31+"",HexStr.substring(i,i+1));
                }

            }
        }
        return map;
    }

將獲取到的數據(33 32)按照空格分開

String[] str = res.split(" ");

兩兩合併生成byte數組。

byte[] b = new byte[17];
for(int i=0;i<17;i++){
    int height = HexStr.indexOf(map1.get(str[i*2]));
    int low = HexStr.indexOf(map1.get(str[i*2+1]));
    b[i] = (byte) (height<<4|low);
}

業務需要,一個byte位代表8個箱門狀態,0-開,1-關

Map<String,Integer> goodsMap = new HashMap<>();
goodsMap.put("1",(b&0x01)==0x01?1:0);
goodsMap.put("2",(b&0x02)==0x02?1:0);
goodsMap.put("3",(b&0x04)==0x04?1:0);
goodsMap.put("4",(b&0x08)==0x08?1:0);
goodsMap.put("5",(b&0x10)==0x10?1:0);
goodsMap.put("6",(b&0x20)==0x20?1:0);
goodsMap.put("7",(b&0x40)==0x40?1:0);
goodsMap.put("8",(b&0x80)==0x80?1:0);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章