问题描述
我的文件中包含以下数据:
I have following data in a file:
我想解码UserData.在将其读取为字符串 comment
时,我正在执行以下操作:
I want to decode the UserData. On reading it as string comment
, I'm doing following:
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:
可能是什么原因?
推荐答案
该图像表明您尝试解码的字符串包含SOH和BEL之类的字符.这些是ASCII控制字符,永远不会出现在Base64编码的字符串中.
The image suggests that the string you are trying to decode contains characters like SOH and BEL. These are ASCII control characters, and will not ever appear in a Base64 encoded string.
(Base64通常由字母,数字和 +
, \
和 =
组成.有一些变体格式,但控制字符是从未包括在内.)
(Base64 typically consists of letters, digits, and +
, \
and =
. There are some variant formats, but control characters are never included.)
这由异常消息确认:
java.lang.IllegalArgumentException: Illegal base64 character 1
SOH字符具有ASCII码1.
The SOH character has ASCII code 1.
结论:
- 您无法像将该字符串解码为Base64一样对其进行解码.这行不通.
- 看上去 字符串根本没有被编码",这在Java中编码"的含义是正常的.
-
在没有明确说明的情况下,我们无法为您提供应做的建议:
- You cannot decode that string as if it was Base64. It won't work.
- It looks like the string is not "encoded" at all ... in the normal sense of what "encoding" means in Java.
We can't advise you on what you should do with it without a clear explanation of:
- (二进制)数据的来源
- 您希望它包含的内容,并且
- 您如何读取数据并将其转换为Java
String
对象:向我们展示执行此操作的代码!
- where the (binary) data comes from,
- what you expected it to contain, and
- how you read the data and turned it into a Java
String
object: show us the code that did that!
这篇关于Java-解码base64-非法的base64字符1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!