String myString;
and I'd like to convert it to another charset, e.g. ISO 8859-2.
How to do it?
thanks in advance
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());
}
}
}
See http://mindprod.com/jgloss/native2ascii.html
http://mindprod.com/jgloss/encoding.html
http://mindprod.com/jgloss/conversion.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
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