[英]Java - decode base64 - Illegal base64 character 1
I have following data in a file: 我在文件中有以下数据:
I want to decode the UserData. 我想解码UserData。 On reading it as string
comment
, I'm doing following: 在将其读取为字符串
comment
,我正在执行以下操作:
String[] split = comment.split("=");
if(split[0].equals("UserData")) {
System.out.println(split[1]);
byte[] callidArray = Arrays.copyOf(java.util.Base64.getDecoder().decode(split[1]), 9);
System.out.println("UserData:" + Hex.encodeHexString(callidArray).toString());
}
But I'm getting the following exception: 但是我收到以下异常:
java.lang.IllegalArgumentException: Illegal base64 character 1
java.lang.IllegalArgumentException:非法的base64字符1
What could be the reason? 可能是什么原因?
The image suggests that the string you are trying to decode contains characters like SOH and BEL. 该图像表明您尝试解码的字符串包含SOH和BEL之类的字符。 These are ASCII control characters, and will not ever appear in a Base64 encoded string.
这些是ASCII控制字符,永远不会出现在Base64编码的字符串中。
(Base64 typically consists of letters, digits, and +
, \\
and =
. There are some variant formats, but control characters are never included.) (Base64通常由字母,数字和
+
, \\
和=
。虽然有一些变体格式,但从不包含控制字符。)
This is confirmed by the exception message: 异常消息确认了这一点:
java.lang.IllegalArgumentException: Illegal base64 character 1
The SOH character has ASCII code 1. SOH字符具有ASCII码1。
Conclusions: 结论:
We can't advise you on what you should do with it without a clear explanation of: 我们不能,你应该用它做什么没有明确的解释劝你:
String
object: show us the code that did that! String
对象:向我们展示执行此操作的代码! The UserData field in the picture in the question actually contains Bytes representation of Hexadecimal characters. 问题图片中的UserData字段实际上包含十六进制字符的字节表示。
So, I don't need to decode Base64. 因此,我不需要解码Base64。 I need to copy the string to a byte array and get equivalent hexadecimal characters of the byte array.
我需要将字符串复制到字节数组,并获取字节数组的等效十六进制字符。
String[] split = comment.split("=");
if(split[0].equals("UserData")) {
System.out.println(split[1]);
byte[] callidArray = Arrays.copyOf(split[1].getBytes(), 9);
System.out.println("UserData:" + Hex.encodeHexString(callidArray).toString());
}
Output: UserData:010a20077100000000
输出:
UserData:010a20077100000000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.