Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to convert String from one charset to another

2 views
Skip to first unread message

meh...@gmail.com

unread,
Aug 3, 2007, 8:34:33 AM8/3/07
to
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

Amit Jain

unread,
Aug 3, 2007, 8:53:34 AM8/3/07
to

Mike Schilling

unread,
Aug 3, 2007, 1:05:10 PM8/3/07
to
meh...@gmail.com wrote:
> 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());
}
}
}


meh...@gmail.com

unread,
Aug 4, 2007, 5:15:26 AM8/4/07
to
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

Roedy Green

unread,
Aug 4, 2007, 7:32:36 AM8/4/07
to
On Fri, 03 Aug 2007 12:34:33 -0000, meh...@gmail.com wrote, quoted or
indirectly quoted someone who said :

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

Oliver Wong

unread,
Aug 7, 2007, 10:29:10 AM8/7/07
to

<meh...@gmail.com> wrote in message
news:1186218926.3...@57g2000hsv.googlegroups.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


0 new messages