由于项目中经常遇到与设备进行交互获取数据,不仅有Python版本、还有Java版本,还可能会有C#版本,在此做个记录。
一、十六进制转换工具类
主要包含十六进制字符串转ASCII,ASCII转十六进制字符串以及十六进制字符串转Byte数组等方法:
/** * Created by wly on 2018/4/17. */ public class HexConvert { public static String convertStringToHex(String str){ char[] chars = str.toCharArray(); StringBuffer hex = new StringBuffer(); for(int i = 0; i < chars.length; i++){ hex.append(Integer.toHexString((int)chars[i])); } return hex.toString(); } public static String convertHexToString(String hex){ StringBuilder sb = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); for( int i=0; i<hex.length()-1; i+=2 ){ String s = hex.substring(i, (i + 2)); int decimal = Integer.parseInt(s, 16); sb.append((char)decimal); sb2.append(decimal); } return sb.toString(); } public static byte[] hexStringToBytes(String hexString) { if (hexString == null || hexString.equals("")) { return null; } // toUpperCase将字符串中的所有字符转换为大写 hexString = hexString.toUpperCase(); int length = hexString.length() / 2; // toCharArray将此字符串转换为一个新的字符数组。 char[] hexChars = hexString.toCharArray(); byte[] d = new byte[length]; for (int i = 0; i < length; i++) { int pos = i * 2; d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); } return d; } //返回匹配字符 private static byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } //将字节数组转换为short类型,即统计字符串长度 public static short bytes2Short2(byte[] b) { short i = (short) (((b[1] & 0xff) << 8) | b[0] & 0xff); return i; } //将字节数组转换为16进制字符串 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; } public static void main(String[] args) { System.out.println("======ASCII码转换为16进制======"); String str = "*00007VERSION\\n1$"; System.out.println("字符串: " + str); String hex = HexConvert.convertStringToHex(str); System.out.println("====转换为16进制=====" + hex); System.out.println("======16进制转换为ASCII======"); System.out.println("Hex : " + hex); System.out.println("ASCII : " + HexConvert.convertHexToString(hex)); byte[] bytes = HexConvert.hexStringToBytes( hex ); System.out.println(HexConvert.BinaryToHexString( bytes )); } }
二、接收数据
public class UdpReceiverThread { public static void main(String[] args) throws Exception { // 定义一个接收端,并且指定了接收的端口号 DatagramSocket socket = new DatagramSocket(6070); while (true) { byte[] buf = new byte[1024*5]; // 解析数据包 DatagramPacket packet = new DatagramPacket(buf, buf.length); socket.receive(packet); String ip = packet.getAddress().getHostAddress(); buf = packet.getData(); //将字节数组转换为16进制字符串 String hexString = HexConvert.BinaryToHexString( buf );//含有空格,如:2A 30 30 30 30 37 56 45 52 53 49 4F 4E 5C 6E 31 24 hexString = hexString.replace( " ","" );//去除空格 String asc = HexConvert.convertHexToString( hexString );//转为ASCII,如:*00007VERSION\n1$ System.out.println("收到 " + ip + " 发来的消息:" + asc); } } }
三、发送数据
public class UDPSenderThread { public static void main(String[] args) throws Exception { DatagramSocket socket = new DatagramSocket(); String serial = "*00007VERSION\\n1$";//串口字符串 String hex = HexConvert.convertStringToHex(serial);//转化为十六进制字符串:2a303030303756455253494f4e5c6e3124 byte[] buf = HexConvert.hexStringToBytes( hex );//将十六进制字符串转为字节数组 //将数据打包 DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.11.139"), 6070); socket.send(packet); socket.close(); } }
在此基础上进行封装、抽象后即可处理十六进制数据的传输与接收问题。
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦