how to convert String from one charset to another

M

mehafi

Hi,
I've got a String, which charset is e.g. ISO 8859-1:

String myString;

and I'd like to convert it to another charset, e.g. ISO 8859-2.
How to do it?

thanks in advance
 
M

Mike Schilling

Hi,
I've got a String, which charset is e.g. ISO 8859-1:

String myString;

Not really. You've got a String that consists of Unicode characters.
(UTF-16 characters, to be very precise.) All strings in Java are Unicode.
What you probalby mean is that you have a String that was initially created
from bytes that were encoded in ISO 8859-1.
and I'd like to convert it to another charset, e.g. ISO 8859-2.
How to do it?

byte[] iso88592Bytes = myString.getBytes("ISO-8859-2");

assuming that your JRE supports 8859-2 If not, this will throw an
UnsupportedEncodingException. Here's a handy program to list all of the
encodings Java supports:

import java.util.*;
import java.nio.charset.*;

class Charsets
{
public static void main(String[] args)
{
List names = new ArrayList();
Iterator cs = Charset.availableCharsets().values().iterator() ;
while (cs.hasNext())
{
names.add(((Charset)cs.next()).name());
}
Collections.sort(names);
Iterator nameIter = names.iterator() ;
while (nameIter.hasNext())
{
System.out.println(nameIter.next());
}
}
}
 
M

mehafi

Thans to all for your posts.
Becauose ISO-8859-1 and ISO-8859-2 are difrent only on 6 positions
( meaning polish national characters ) I wrote a code, which:
1) convert String to StringBuffer,
2) searching in StringBuffer this 6 characters and repleacing them
witch propper charset
 
O

Oliver Wong

Thans to all for your posts.
Becauose ISO-8859-1 and ISO-8859-2 are difrent only on 6 positions
( meaning polish national characters ) I wrote a code, which:
1) convert String to StringBuffer,
2) searching in StringBuffer this 6 characters and repleacing them
witch propper charset

Your program is probably buggy then. It may work on YOUR particular
computer, but it may not work when moved onto another computer where the
default encodings differ. Re-read Mike Schiling's post, particularly the
part that goes:

<quote>
All strings in Java are Unicode.
What you probalby mean is that you have a String that was initially
created
from bytes that were encoded in ISO 8859-1.
</quote>

Given your problem description, your best bet is probably not to use
the String class at all, and work entirely with byte[].

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top