Base64 UTF-32解码在Java中的工作不如预期。

huangapple go评论87阅读模式
英文:

Base64 UTF-32 decoding in java not working as expected

问题

I have Base64 UTF-32 encoded string, while decoding it comes with white spaces.
I am using org.apache.commons.codec library.

For encoding below code is used and working fine as expected
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"));
this gives me Base64 encoded string as 
AAAAVAAAAGgAAABpAAAAcwAAACAAAABpAAAAcwAAACAAAABVAAAAVAAAAEYAAAAtAAAAMwAAADIAAAAgAAAAZQAAAG4AAABjAAAAbwAAAGEAAABkAAAAaQAAAG4AAABnAAAAIAAAAHQAAABlAAAAcwAAAHQ=
I want to decode above string
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"));
Output received as below, which not correct
T   h   i   s       i   s       U   T   F   -   3   2        e   n   c   o   a 
  d   i   n   g        t   e   s   t
How to get it as original string after decoding like below value
This is UTF-32 encoding test","UTF-32
英文:

I have Base64 UTF-32 encoded string, while decoding it comes with white spaces.
I am using org.apache.commons.codec library.

For encoding below code is used and working fine as expected

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"));

this gives me Base64 encoded string as

AAAAVAAAAGgAAABpAAAAcwAAACAAAABpAAAAcwAAACAAAABVAAAAVAAAAEYAAAAtAAAAMwAAADIAAAAgAAAAZQAAAG4AAABjAAAAbwAAAGEAAABkAAAAaQAAAG4AAABnAAAAIAAAAHQAAABlAAAAcwAAAHQ=

I want to decode above string

	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"));

Output received as below, which not correct

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

How to get it as original string after decoding like below value
This is UTF-32 encoding test","UTF-32

答案1

得分: 2

你忘记将字符编码传递给String构造函数,所以它正在使用平台默认的字符编码创建字符串。使用:

return new String(actualByte, utfType);
英文:

You forgot to pass the character encoding to the String constructor, so it's creating the string using the platform default character encoding. Use:

return new String(actualByte, utfType);

huangapple
  • 本文由 发表于 2020年7月25日 22:56:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63089796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定