在Java中将字符串ISO8859-1转换为UTF-8

时间:2012-05-08 10:50:07

标签: android utf-8 iso-8859-1 quoted-printable

我有一个Q-encoded字符串变量:

String s = "=?ISO-8859-1?Q?Merve_G=FCl?=";

我想在屏幕上用真实字符:“MerveGül”写它。

我该怎么做?

@Nick

String string = "=?ISO-8859-1?Q?Merve_G=FCl?=";
QCodec q = new QCodec();
        try 
        {
            q.decode(string);
            System.out.println(q.decode(kimdenIsim));
        } catch (DecoderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

我收到此错误:

05-08 11:22:39.287: W/System.err(1142): org.apache.commons.codec.DecoderException: RFC 1522 violation: malformed encoded content
05-08 11:22:40.448: W/System.err(1142):     at org.apache.commons.codec.net.RFC1522Codec.decodeText(RFC1522Codec.java:102)
05-08 11:22:40.448: W/System.err(1142):     at org.apache.commons.codec.net.QCodec.decode(QCodec.java:230)
05-08 11:22:40.457: W/System.err(1142):     at com.mobil.eposta.GoruntuleActivity.EksizPosta(GoruntuleActivity.java:138)
05-08 11:22:40.457: W/System.err(1142):     at com.mobil.eposta.GoruntuleActivity.onCreate(GoruntuleActivity.java:52)
05-08 11:22:40.477: W/System.err(1142):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-08 11:22:40.477: W/System.err(1142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-08 11:22:40.477: W/System.err(1142):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-08 11:22:40.497: W/System.err(1142):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-08 11:22:40.497: W/System.err(1142):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-08 11:22:40.507: W/System.err(1142):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 11:22:40.517: W/System.err(1142):     at android.os.Looper.loop(Looper.java:123)
05-08 11:22:40.517: W/System.err(1142):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-08 11:22:40.517: W/System.err(1142):     at java.lang.reflect.Method.invokeNative(Native Method)
05-08 11:22:40.517: W/System.err(1142):     at java.lang.reflect.Method.invoke(Method.java:521)
05-08 11:22:40.517: W/System.err(1142):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-08 11:22:40.537: W/System.err(1142):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-08 11:22:40.537: W/System.err(1142):     at dalvik.system.NativeStart.main(Native Method)

第4行错误显示:q.decode(string);

3 个答案:

答案 0 :(得分:2)

根据this回答是否可能这样。你需要JavaMail。我刚刚成功测试了它。

import javax.mail.internet.MimeUtility;

String s = "=?ISO-8859-1?Q?Merve_G=FCl?=";
String string = MimeUtility.decodeText(s);

答案 1 :(得分:1)

Apache Commons Codec有一个QCodec类用于解码此格式。只需将库添加到项目中即可。

//String s must start with "=?" and end with "?="
QCodec q = new QCodec();
q.decode(s);

答案 2 :(得分:-2)

试试此代码

try {
    // Convert from Unicode to UTF-8
    String string = "abc\u5639\u563b";
    byte[] utf8 = string.getBytes("UTF-8");

    // Convert from UTF-8 to Unicode
    string = new String(utf8, "UTF-8");
} catch (UnsupportedEncodingException e) {
}