java中的Base64 UTF-32解码未按预期工作



我有Base64 UTF-32编码的字符串,解码时带有空格。我正在使用org.apache.commons.codec库。

对于以下编码,使用代码并按预期正常工作

public static String encodeBase64(String encodeString,String utfType){
try {
return new String(Base64.encodeBase64String(encodeString.getBytes(utfType)));
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
System.out.println(encodeBase64("This is UTF-32 encoading test","UTF-32"));

这给了我作为的Base64编码字符串

AAAAVAAAAGgAAABpAAAAcwAAACAAAABpAAAAcwAAACAAAABVAAAAVAAAAEYAAAAtAAAAMwAAADIAAAAgAAAAZQAAAG4AAABjAAAAbwAAAGEAAABkAAAAaQAAAG4AAABnAAAAIAAAAHQAAABlAAAAcwAAAHQ=

我想解码以上字符串

public static String decodeBase64(String decodeString,String utfType){
try {
byte[] actualByte = java.util.Base64.getDecoder() .decode(decodeString);
return new String(actualByte);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
System.out.println(decodeBase64("AAAAVAAAAGgAAABpAAAAcwAAACAAAABpAAAAcwAAACAAAABVAAAAVAAAAEYAAAAtAAAAMwAAADIAAAAgAAAAZQAAAG4AAABjAAAAbwAAAGEAAABkAAAAaQAAAG4AAABnAAAAIAAAAHQAAABlAAAAcwAAAHQ=","UTF-32"));

收到的输出如下,不正确

T   h   i   s       i   s       U   T   F   -   3   2        e   n   c   o   a 
d   i   n   g        t   e   s   t

如何在解码后将其作为原始字符串如下值这是UTF-32编码测试"UTF-32

您忘记将字符编码传递给String构造函数,因此它使用平台默认的字符编码创建字符串。用途:

return new String(actualByte, utfType);

相关内容

  • 没有找到相关文章

最新更新