💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
~~~ package com.example.mqttdemo; /** * 16进制异或处理 * BCC校验(异或校验) */ public class Aa { public static void main(String args[]) { // String str = "B10211223344011912240100000000000000AABBCCDD1A2002040A00000000000000"; // C608000050503C00000000 返回 F2 String str = "C608000050503C00000000"; byte[] bytes = Aa.toByteArray(str); String bcc = Aa.getBCC(bytes); System.out.println(bcc); } private static String getBCC(byte[] data) { String ret = ""; byte BCC[] = new byte[1]; for (int i = 0; i < data.length; i++) { BCC[0] = (byte) (BCC[0] ^ data[i]); } String hex = Integer.toHexString(BCC[0] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } ret += hex.toUpperCase(); return ret; } public static byte[] toByteArray(String hexString) { hexString = hexString.toLowerCase(); final byte[] byteArray = new byte[hexString.length() / 2]; int k = 0; for (int i = 0; i < byteArray.length; i++) { byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff); byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff); byteArray[i] = (byte) (high << 4 | low); k += 2; } return byteArray; } } ~~~