我得到一个base64字符串。如何将其转换为十六进制。 我试过了followind,但它没有工作
String guid = "YxRfXk827kPgkmMUX15PNg==";
byte[] decoded = Base64.decodeBase64(guid);
String hexString = Hex.encodeHexString(decoded);
System.out.println(hexString);
答案 0 :(得分:1)
String guid = "YxRfXk827kPgkmMUX15PNg==";
byte[] decoded = Base64.decodeBase64(guid);
String result = HexUtil.toHex(decoded);
hexUtil类:
public class HexUtil{
private static final char[] DIGITS = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
public static final String toHex(byte[] data) {
final StringBuilder sb = new StringBuilder();
for (byte d : data) {
sb.append(DIGITS[(d >>> 4) & 0x0F]);
sb.append(DIGITS[d & 0x0F]);
}
return sb.toString();
}
}