英文:
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);
专注分享java语言的经验与见解,让所有开发者获益!

评论