Java 方法实现 UTF-8 格式车牌字符串转 16 进制
你可以使用以下方法来实现:
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String licensePlate = '湘AX2P12';
String hexString = getHexOutput(licensePlate);
System.out.println(hexString);
}
public static String getHexOutput(String licensePlate) {
byte[] bytes = licensePlate.getBytes(StandardCharsets.UTF_8);
byte[] hexBytes = Arrays.copyOf(bytes, 16);
return bytesToHex(hexBytes);
}
public static String bytesToHex(byte[] bytes) {
StringBuilder hexString = new StringBuilder();
for (byte b : bytes) {
hexString.append(String.format('%02X', b));
}
return hexString.toString();
}
}
这段代码中,我们首先将车牌字符串 'licensePlate' 转换为 UTF-8 格式的字节数组 'bytes'。然后,我们将字节数组 'bytes' 复制到长度为 16 的新数组 'hexBytes' 中,并在末尾补 0。最后,我们将新数组 'hexBytes' 转换为 16 进制字符串并返回。
输出结果为:'E6B9B8 4158 3250 3132 0000 0000 0000 0000'。
原文地址: https://www.cveoy.top/t/topic/pfYp 著作权归作者所有。请勿转载和采集!