Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

how to convert String from one charset to another

Thread view: 
mehafi@gmail.com - 03 Aug 2007 13:34 GMT
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 - 03 Aug 2007 13:53 GMT
http://www.exampledepot.com/egs/java.nio.charset/ConvertChar.html

Amit Jain
Mike Schilling - 03 Aug 2007 18:05 GMT
> 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());
       }
   }
}
mehafi@gmail.com - 04 Aug 2007 10:15 GMT
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
Oliver Wong - 07 Aug 2007 15:29 GMT
> 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
Roedy Green - 04 Aug 2007 12:32 GMT
>Hi,
>I've got a String, which charset is e.g. ISO 8859-1:
[quoted text clipped - 3 lines]
>and I'd like to convert it to another charset, e.g. ISO 8859-2.
>How to do it?

See http://mindprod.com/jgloss/native2ascii.html
http://mindprod.com/jgloss/encoding.html
http://mindprod.com/jgloss/conversion.html

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.