Thursday, February 9, 2012

[android-developers] Problem with UTF-16 Android String to xml UTF-8 string conversion

Hello,

I am working with some native code which parses an xml file to give me
a String in my Android which is a UTF-16 string.

Now its output is as expected with no extra unwanted charachters when
I print it in logs. But when I try to display the same string in my UI
which has a TextView then it comes with unwanted charachters and the
whole purpose is defeated. So I want to understand how can I achieve
this.


Code flow: Some attribute in xml file
<span dir="ltr">&lt; RES<span dir="rlo">&lt;-- TLU</span>

following is parsed by native code to be interpreted as
< RESULT --> ( outputs this in logs )
which is my expected outcome, but I get something like <RES(unwanted
char)<TLU as output.

I am using setText function of TextView to set CharSequence and as xml
TextView is UTF-8
I tried using a CharBuffer to extract "UTF-8" characters from String
using Charset class as -

Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(inString));
CharBuffer cbuf = decoder.decode(bbuf);

tv.setText(cbuf);

I am not able to understand the exact problem. But to me it looks a
problem with encoding.

Thanks for any suggestions.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment