Mohammed Housseyn Taleb
Mohammed Housseyn Taleb

Reputation: 1828

How can i convert a string to all possible charset?

I was wondering how should i do to convert a string in Arabic to all possible charset supported by java is it possible ? I searched in the net and i found that java supports this set of encoding and what i would do is like:

      enter the string : hello <-- in arabic
            charset utf 8 ==> XXXXX  <---- this is the converted string for utf 8
            charset iso8859_1 ==> XXXXX  <----  this is the converted string for iso8859_1

after the answer of VYTAS i made this code :

public static void main(String[] args) {
    // TODO code application logic here
    SortedMap<String, Charset> availableCharsets = Charset.availableCharsets();
    Set<String> keySet = availableCharsets.keySet();
    for (String key : keySet) {
        try {
        System.out.println(new String(availableCharsets.get(key).encode("عباس").array(), availableCharsets.get(key)) );

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

but the output is only squares and '?' and sometimes the Arabic String, is what i have done correct ? because I m expecting something like in this website using the word :'عباس'.

thanks

Upvotes: 2

Views: 281

Answers (1)

Vytautas
Vytautas

Reputation: 462

Once you have a String in Arabic, you can do this:

Charset.forName("UTF-8").encode(str);

where 'UTF-8' is canonical name of the charset for java.nio API,
and 'str' is your String in Arabic.

Upvotes: 3

Related Questions