java.lang.IllegalArgumentException:错误的base-64 [英] java.lang.IllegalArgumentException: bad base-64
问题描述
我正在尝试使用android.util.Base64编码和解码String,但是它给出了严重的base-64错误.
I am trying to encode and decode a String using android.util.Base64, but it gives a bad base-64 error.
出现问题的代码是:
private byte[] base64ToByte(String str) throws IOException {
Log.i("encription", str);
byte[] returnbyteArray = Base64.decode(str, Base64.URL_SAFE);
return returnbyteArray;
}
错误logcat是:
08-09 13:02:18.589: E/AndroidRuntime(29827): Process: com.example.maptest, PID: 29827
08-09 13:02:18.589: E/AndroidRuntime(29827): java.lang.IllegalArgumentException: bad base-64
08-09 13:02:18.589: E/AndroidRuntime(29827): at android.util.Base64.decode(Base64.java:161)
08-09 13:02:18.589: E/AndroidRuntime(29827): at android.util.Base64.decode(Base64.java:136)
08-09 13:02:18.589: E/AndroidRuntime(29827): at android.util.Base64.decode(Base64.java:118)
08-09 13:02:18.589: E/AndroidRuntime(29827): at com.example.maptest.security.Encription.base64ToByte(Encription.java:116)
08-09 13:02:18.589: E/AndroidRuntime(29827): at com.example.maptest.security.Encription.encode(Encription.java:103)
08-09 13:02:18.589: E/AndroidRuntime(29827): at android.os.Handler.dispatchMessage(Handler.java:102)
08-09 13:02:18.589: E/AndroidRuntime(29827): at android.os.Looper.loop(Looper.java:136)
08-09 13:02:18.589: E/AndroidRuntime(29827): at android.app.ActivityThread.main(ActivityThread.java:5081)
08-09 13:02:18.589: E/AndroidRuntime(29827): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 13:02:18.589: E/AndroidRuntime(29827): at java.lang.reflect.Method.invoke(Method.java:515)
08-09 13:02:18.589: E/AndroidRuntime(29827): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
08-09 13:02:18.589: E/AndroidRuntime(29827): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-09 13:02:18.589: E/AndroidRuntime(29827): at dalvik.system.NativeStart.main(Native Method)
输入(代码中的str字符串)如下所示:
The input(the str String from the code) looks like this:
08-09 13:02:18.539: I/encription(29827): 26.919047981500626
它是使用以下代码转换为字符串的双精度形式:
It is a double converted to a string, using:
String.valueOf(number)
该错误在所有编码标记(DEFAULT,NO_WRAP等)下均持续存在,对您的帮助将不胜感激,谢谢.
The error is persistent with all encoding flags (DEFAULT, NO_WRAP, etc), any help would be apreciated, thank you.
推荐答案
您正在尝试解码 26.919047981500626
,但这是您无法做到的.因为它不是有效的base64编码字符串.
You are trying to decode 26.919047981500626
which you can't. Because it's not a valid base64 encoded string.
当放入此在线base64解码器时,会出现以下错误:>
When put into this online base64 decoder, it gives this error:
输入不是有效的Base-64字符串,因为它包含非base 64字符串 字符,两个以上的填充字符或非法字符 在填充字符之间.
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
更新:
如果您想知道已编码的base64字符串的有效格式,请查看维基百科文章以及此答案,其中显示了C#中的base64验证程序代码.
If you want to know the valid formatting of a encoded base64 string, take a look at the table in the wikipedia article and also this answer which shows a base64 validator code in C#.
这篇关于java.lang.IllegalArgumentException:错误的base-64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!