package com.inspect.nvr.utils; import lombok.extern.slf4j.Slf4j; import java.nio.charset.StandardCharsets; @Slf4j public class StringHexConverter { // 字符串 -> 十六进制字符串 public static String toHex(String input) { byte[] bytes = input.getBytes(StandardCharsets.UTF_8); StringBuilder hex = new StringBuilder(bytes.length * 2); for (byte b : bytes) { hex.append(String.format("%02x", b)); } return hex.toString(); } // 十六进制字符串 -> 原始字符串 public static String fromHex(String hex) { int length = hex.length(); byte[] bytes = new byte[length / 2]; for (int i = 0; i < length; i += 2) { bytes[i / 2] = (byte) Integer.parseInt(hex.substring(i, i + 2), 16); } return new String(bytes, StandardCharsets.UTF_8); } }