| New file |
| | |
| | | package com.genersoft.iot.vmp.netty.config; |
| | | |
| | | import io.netty.buffer.ByteBuf; |
| | | import io.netty.channel.ChannelHandlerContext; |
| | | import io.netty.handler.codec.ByteToMessageDecoder; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 自定义解析 |
| | | */ |
| | | @Component |
| | | public class MyDecoder extends ByteToMessageDecoder { |
| | | |
| | | @Override |
| | | protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception { |
| | | //创建字节数组,buffer.readableBytes可读字节长度 |
| | | byte[] b = new byte[buffer.readableBytes()]; |
| | | //复制内容到字节数组b |
| | | buffer.readBytes(b); |
| | | //字节数组转字符串 |
| | | String str = new String(b); |
| | | |
| | | System.out.println(str); |
| | | |
| | | out.add(bytesToHexString(b)); |
| | | } |
| | | |
| | | public String bytesToHexString(byte[] bArray) { |
| | | StringBuffer sb = new StringBuffer(bArray.length); |
| | | String sTemp; |
| | | for (int i = 0; i < bArray.length; i++) { |
| | | sTemp = Integer.toHexString(0xFF & bArray[i]); |
| | | if (sTemp.length() < 2) |
| | | sb.append(0); |
| | | sb.append(sTemp.toUpperCase()); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | public static String toHexString1(byte[] b) { |
| | | StringBuffer buffer = new StringBuffer(); |
| | | for (int i = 0; i < b.length; ++i) { |
| | | buffer.append(toHexString1(b[i])); |
| | | } |
| | | return buffer.toString(); |
| | | } |
| | | |
| | | public static String toHexString1(byte b) { |
| | | String s = Integer.toHexString(b & 0xFF); |
| | | if (s.length() == 1) { |
| | | return "0" + s; |
| | | } else { |
| | | return s; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 16进制字符串转换为字符串 |
| | | * |
| | | * @param s |
| | | * @return |
| | | */ |
| | | public static String hexStringToString(String s) { |
| | | if (s == null || s.equals("")) { |
| | | return null; |
| | | } |
| | | s = s.replace(" ", ""); |
| | | byte[] baKeyword = new byte[s.length() / 2]; |
| | | for (int i = 0; i < baKeyword.length; i++) { |
| | | try { |
| | | baKeyword[i] = (byte) (0xff & Integer.parseInt( |
| | | s.substring(i * 2, i * 2 + 2), 16)); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | try { |
| | | s = new String(baKeyword, "gbk"); |
| | | new String(); |
| | | } catch (Exception e1) { |
| | | e1.printStackTrace(); |
| | | } |
| | | return s; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Hex字符串转byte |
| | | * @param inHex 待转换的Hex字符串 |
| | | * @return 转换后的byte |
| | | */ |
| | | public static byte hexToByte(String inHex){ |
| | | return (byte)Integer.parseInt(inHex,16); |
| | | } |
| | | |
| | | /** |
| | | * Hex字符串转byte |
| | | * @param inHex 待转换的Hex字符串 |
| | | * @return 转换后的byte |
| | | */ |
| | | public static short hexTohort(String inHex){ |
| | | return (short)Integer.parseInt(inHex,16); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * hex字符串转byte数组 |
| | | * @param inHex 待转换的Hex字符串 |
| | | * @return 转换后的byte数组结果 |
| | | */ |
| | | public static byte[] hexToByteArray(String inHex){ |
| | | int hexlen = inHex.length(); |
| | | byte[] result; |
| | | if (hexlen % 2 == 1){ |
| | | //奇数 |
| | | hexlen++; |
| | | result = new byte[(hexlen/2)]; |
| | | inHex="0"+inHex; |
| | | }else { |
| | | //偶数 |
| | | result = new byte[(hexlen/2)]; |
| | | } |
| | | int j=0; |
| | | for (int i = 0; i < hexlen; i+=2){ |
| | | result[j]=hexToByte(inHex.substring(i,i+2)); |
| | | j++; |
| | | } |
| | | return result; |
| | | } |
| | | } |