记录与硬件通信的Base64与16进制字符串转换
/** * 202212 * <p> * base64ToHex * HexToBase64 * </p> */ public class Base64Utils { /** * base64编码格式的16进制转16进制字符串 * @param base64HexString base64编码格式的16进制 * @return 16进制字符串 */ public static String base64ToHex(String base64HexString) { try { // 解码 byte[] decode = Base64.getDecoder().decode(base64HexString); // 返回 16进制字符编码 return Hex.encodeHexString(decode); } catch (RuntimeException e) { // 抛出自定义异常 throw new RuntimeException("base64编码格式的16进制转16进制字符串错误,参数格式错误!"); } } /** * 16进制字符串转Base64字符串 * @param hexString * @return */ public static String hexToBase64(String hexString) { try { // 16进制解码 byte[] bytes = Hex.decodeHex(hexString); // 返回 编码成base64 return Base64.getEncoder().encodeToString(bytes); } catch (Exception e) { // 抛出自定义异常 throw new RuntimeException("16进制转base64编码错误, 参数格式错误!"); } } }