You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

28 lines
875 B

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);
}
}