当前位置: 技术问答>java相关
如果把一个字符串按指定方式解码?
来源: 互联网 发布时间:2015-04-14
本文导语: 比如String f为gb2312方式编码,我如何获得f解码后的字符串? | { if(!s.startsWith("=?")) return s; int i = 2; int j; if((j = s.indexOf(63, i)) == -1) ...
比如String f为gb2312方式编码,我如何获得f解码后的字符串?
|
{
if(!s.startsWith("=?"))
return s;
int i = 2;
int j;
if((j = s.indexOf(63, i)) == -1)
return s;
String s1 = (s.substring(i, j));
i = j + 1;
if((j = s.indexOf(63, i)) == -1)
return s;
String s2 = s.substring(i, j);
i = j + 1;
if((j = s.indexOf("?=", i)) == -1)
return s;
String s3 = s.substring(i, j);
try
{
ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(s3.getBytes());
Object obj;
if(s2.equalsIgnoreCase("B"))
obj = new com.sun.mail.util.BASE64DecoderStream(bytearrayinputstream);
else
if(s2.equalsIgnoreCase("Q"))
obj = new com.sun.mail.util.QDecoderStream(bytearrayinputstream);
else
return s;
int k = bytearrayinputstream.available();
byte abyte0[] = new byte[k];
k = ((InputStream)(obj)).read(abyte0, 0, k);
return new String(abyte0, 0, k);
}
catch(Exception ex)
{
return s;
}
}
if(!s.startsWith("=?"))
return s;
int i = 2;
int j;
if((j = s.indexOf(63, i)) == -1)
return s;
String s1 = (s.substring(i, j));
i = j + 1;
if((j = s.indexOf(63, i)) == -1)
return s;
String s2 = s.substring(i, j);
i = j + 1;
if((j = s.indexOf("?=", i)) == -1)
return s;
String s3 = s.substring(i, j);
try
{
ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(s3.getBytes());
Object obj;
if(s2.equalsIgnoreCase("B"))
obj = new com.sun.mail.util.BASE64DecoderStream(bytearrayinputstream);
else
if(s2.equalsIgnoreCase("Q"))
obj = new com.sun.mail.util.QDecoderStream(bytearrayinputstream);
else
return s;
int k = bytearrayinputstream.available();
byte abyte0[] = new byte[k];
k = ((InputStream)(obj)).read(abyte0, 0, k);
return new String(abyte0, 0, k);
}
catch(Exception ex)
{
return s;
}
}
|
String sd = "Asdf";
String s = new String(sd.getBytes(),"gb2312");
String s = new String(sd.getBytes(),"gb2312");
|
String f = "something";
byte[] decoded = f.getBytes("gb2312");
解码以后生成byte数组
重新构造新的字符串:
String newf = new String(decoded,"iso-8859-1");
byte[] decoded = f.getBytes("gb2312");
解码以后生成byte数组
重新构造新的字符串:
String newf = new String(decoded,"iso-8859-1");