1

I have a Q-encoded string variable:

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

And I want to write it on screen with true characters: "Merve Gül".

How can I do that?

@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();
        } 

I am getting this error :

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)

4th error line shows : q.decode(string);

Merve Gül
  • 1,377
  • 6
  • 23
  • 40
  • There is already discussion about this here : http://stackoverflow.com/questions/652161/how-do-i-convert-between-iso-8859-1-and-utf-8-in-java – UVM May 08 '12 at 10:57
  • I already tried all samples at this page, but I couldnt find any solution – Merve Gül May 08 '12 at 11:03
  • Could anyone answering please look at this before giving another irrelevant response: http://en.wikipedia.org/wiki/MIME#Encoded-Word – Nick May 08 '12 at 11:09
  • What am I going to do so if the problem is like in wikipedia – Merve Gül May 08 '12 at 11:10
  • 1
    so what is the value of `kimdenIsKim` - since you are calling `QCodec#decode(String)` twice. The format of the `string` attribute you pasted into your question appears correct and parses with commons-codec 1.6 btw. – Jens May 08 '12 at 12:15
  • String string = String kimdenIsim . Sorry, I changed the code for good understandability than I forgot it into the logcat – Merve Gül May 08 '12 at 13:02
  • How can I use decodeText @Rup ? – Merve Gül May 08 '12 at 13:06
  • Oops, didn't spot it was protected. Sorry. No, decode just wraps decodeText. – Rup May 08 '12 at 13:08
  • 1
    [The exception "Malformed encoded content"](http://svn.apache.org/repos/asf/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java) means your string does not start with "=?" and end with "?=". It looks like your example does. Are you definitely passing it a string in this form? – Rup May 08 '12 at 13:12
  • @Rup I fix the error with apply the String like in my question. Because in the real application the string is not hold like that. String kimdenIsim = kimden.substring(0,"<"); QCodec q = new QCodec(); try { System.out.println(q.decode(kimdenIsim)); } catch (DecoderException e) { // TODO Auto-generated catch block e.printStackTrace(); } This code give the error. But I dont know why:S – Merve Gül May 08 '12 at 13:34

3 Answers3

2

according to this answer is it possible this way. You need JavaMail for that. I just tested it successfully.

import javax.mail.internet.MimeUtility;

String s = "=?ISO-8859-1?Q?Merve_G=FCl?=";
String string = MimeUtility.decodeText(s);
Community
  • 1
  • 1
Malte
  • 1,937
  • 1
  • 15
  • 20
1

Apache Commons Codec has a QCodec class for decoding this format. Just add the library to your project.

//String s must start with "=?" and end with "?="
QCodec q = new QCodec();
q.decode(s);
Nick
  • 11,475
  • 1
  • 36
  • 47
  • I know , your code is true. But I get error and add it my question – Merve Gül May 08 '12 at 11:29
  • As mentioned by Rup, could you try `decodeText` as I've changed my answer to illustrate? – Nick May 08 '12 at 13:07
  • It is not applicable :S And give this line to me "The method decodeText(String) from the type RFC1522Codec is not visible" – Merve Gül May 08 '12 at 13:11
  • 1
    Yeah, sorry, my bad. decodeText is inherited from RFC1522Codec and QCodec's decode just wraps it - decode is correct here. – Rup May 08 '12 at 13:14
  • 2
    [Here is the code that is throwing the error](http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java?view=markup#l144). You can see that the only check is that your string starts with "=?" and ends with "?=". Are you definitely passing a string in that form? That is however trunk commons-codec - if that's not the problem then please let us know exactly what version you're using so we can check that too. – Rup May 08 '12 at 13:20
  • Thank you so much! I copied the string to another one and it fix it. Thanks again!! – Merve Gül May 08 '12 at 13:27
  • @Rup I fix the error with apply the String like in my question. Because in the real application the string is not hold like that. String kimdenIsim = kimden.substring(0,"<"); QCodec q = new QCodec(); try { System.out.println(q.decode(kimdenIsim)); } catch (DecoderException e) { // TODO Auto-generated catch block e.printStackTrace(); } This code give the error. But I dont know why:S – Merve Gül May 08 '12 at 13:38
-2

Try this code

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) {
}
Jackson Chengalai
  • 3,907
  • 1
  • 24
  • 39