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 « cassé » String
, vous avez fait quelque chose de mal, la conversion d'un String
à un String
dans un autre encodage est defenetely pas la manière aller! Vous pouvez convertir un String
en un byte[]
et vice versa (en raison d'un encodage). En Java String
s sont codés AFAIK avec UTF-16
mais c'est un détail d'implémentation.
Supposons que vous avez un InputStream
, vous pouvez lire dans un byte[]
puis le convertir en un String
en utilisant
byte[] bs = ...;
String s;
try {
s = new String(bs, encoding);
} catch(UnsupportedEncodingException uee) {
uee.printStackTrace();
}
ou encore mieux (grâce à erickson) utiliser InputStreamReader
comme ça:
InputStreamReader isr;
try {
isr = new InputStreamReader(inputStream, encoding);
} catch(UnsupportedEncodingException uee) {
uee.printStackTrace();
}
double possible de [conversion d'encodage en java] (http: // stackoverflow.com/questions/229015/encoding-conversion-in-java) – kamaci