Comment convertir entre ISO-8859 - 1 et UTF-8 en Java?


Quelqu'un sait-il comment convertir une chaîne de ISO-8859-1 en UTF-8 et retour en Java?

Je reçois une chaîne du Web et je l'enregistre dans le RMS (J2ME), mais je veux préserver les caractères spéciaux et obtenir la chaîne du RMS mais avec l'encodage ISO-8859-1. Comment dois-je faire?

Author: Michael Myers, 2009-03-17

5 answers

En général, vous ne pouvez pas faire cela. UTF - 8 est capable d'encoder n'importe quel point de code Unicode. ISO-8859-1 ne peut en traiter qu'une infime fraction. Ainsi, le transcodage d'ISO-8859-1 vers UTF-8 ne pose aucun problème. Revenir en arrière de l'UTF - 8 à l'ISO-8859-1 entraînera l'apparition de "caractères de remplacement" (DEC) dans votre texte lorsque des caractères non pris en charge sont trouvés.

Pour transcoder le texte:

byte[] latin1 = ...
byte[] utf8 = new String(latin1, "ISO-8859-1").getBytes("UTF-8");

Ou

byte[] utf8 = ...
byte[] latin1 = new String(utf8, "UTF-8").getBytes("ISO-8859-1");

Vous pouvez exercer plus de contrôle en utilisant le niveau inférieur Charset les Api. Pour par exemple, vous pouvez déclencher une exception lorsqu'un caractère non encodable est trouvé ou utiliser un caractère différent pour le texte de remplacement.

 90
Author: erickson, 2014-04-18 18:46:00

ce Qui a fonctionné pour moi: ("üzüm bağları" est le bon écrit en turc)

Convertir ISO-8859 - 1 en UTF-8:

String encodedWithISO88591 = "üzüm baÄları";
String decodedToUTF8 = new String(encodedWithISO88591.getBytes("ISO-8859-1"), "UTF-8");
//Result, decodedToUTF8 --> "üzüm bağları"

Convertir UTF - 8 en ISO-8859-1

String encodedWithUTF8 = "üzüm bağları";
String decodedToISO88591 = new String(encodedWithUTF8.getBytes("UTF-8"), "ISO-8859-1");
//Result, decodedToISO88591 --> "üzüm baÄları"
 7
Author: Bahadir Tasdemir, 2016-08-12 08:45:00

Si vous avez un String, vous pouvez le faire:

String s = "test";
try {
    s.getBytes("UTF-8");
} catch(UnsupportedEncodingException uee) {
    uee.printStackTrace();
}

Si vous avez un String' cassé', vous avez fait quelque chose de mal, convertir un String en un String dans un autre encodage n'est defenetely pas la voie à suivre! Vous pouvez convertir un String en un byte[] et vice-versa (étant donné un encodage). En Java, les Stringsont encodés AFAIK avec UTF-16 mais c'est un détail d'implémentation.

, Dire que vous avez un InputStream, vous pouvez lire dans un byte[], puis convertir un String utiliser

byte[] bs = ...;
String s;
try {
    s = new String(bs, encoding);
} catch(UnsupportedEncodingException uee) {
    uee.printStackTrace();
}

Ou encore mieux (merci pour erickson) utilisez InputStreamReader comme ça:

InputStreamReader isr;
try {
     isr = new InputStreamReader(inputStream, encoding);
} catch(UnsupportedEncodingException uee) {
    uee.printStackTrace();
}
 6
Author: Johannes Weiss, 2009-03-16 22:36:19

Voici un moyen facile avec la sortie de Chaîne (j'ai créé une méthode pour ce faire):

public static String (String input){
    String output = "";
    try {
        /* From ISO-8859-1 to UTF-8 */
        output = new String(input.getBytes("ISO-8859-1"), "UTF-8");
        /* From UTF-8 to ISO-8859-1 */
        output = new String(input.getBytes("UTF-8"), "ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return output;
}
// Example
input = "Música";
output = "Música";
 4
Author: JLeon90, 2017-09-05 11:55:43

Apache Commons IO Charsets class peut être utile:

String utf8String = new String(org.apache.commons.io.Charsets.ISO_8859_1.encode(latinString).array())
 0
Author: Alberto Segura, 2017-04-06 13:34:48