dcsimg
Charset conversion problem
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jan_Gifvars
Posted On:   Wednesday, March 12, 2003 03:19 AM

Hi, I try to convert a string from a html page which I know is a ISO-8859-1 16 bit string into a UTF-8 string, but I fail. You can see the code below. I haven't used the package java.nio.charset, so I propably do some stupid things. pString is my ISO* string. Regards, Jan Gifvars pString = ... Charset tCharset = Charset.forName("ISO-8859-1"); CharsetDecoder tDecoder = tCharset.newDecoder(); ByteBuffer tBuff = ByteBuffer.allocate(pString.length()*2); for (int i = 0; i < pString.length(); i++){ tBuff.putChar(pString.charAt(i)); } try { CharBuffer tCbuf = tDecoder.decode(   More>>

Hi,



I try to convert a string from a html page which I know is a ISO-8859-1 16 bit string into a UTF-8 string, but I fail. You can see the code below. I haven't used the package java.nio.charset, so I propably do some stupid things. pString is my ISO* string.



Regards,


Jan Gifvars


			

pString = ...
Charset tCharset = Charset.forName("ISO-8859-1");
CharsetDecoder tDecoder = tCharset.newDecoder();

ByteBuffer tBuff = ByteBuffer.allocate(pString.length()*2);

for (int i = 0; i < pString.length(); i++){
tBuff.putChar(pString.charAt(i));
}

try {

CharBuffer tCbuf = tDecoder.decode(tBuff);
String s = tCbuf.toString();

tCharset = Charset.forName("UTF-8");
CharsetEncoder tEncoder = tCharset.newEncoder();

ByteBuffer tBbuf = tEncoder.encode(tCbuf);
String tResult = new String(tBbuf.array());
return tNewStr; // Returns empty string ""

} catch (Throwable e) {
System.out.println(e);
return null;
}
   <<Less
About | Sitemap | Contact