Sto riscontrando qualche problema nel recuperare testo francese da convertire in UTF8 in modo che possa essere visualizzato correttamente, in una console, in un file di testo o in un elemento della GUI.Codifica caratteri UTF-8 in Java
La stringa originale è
HANDICAP╔ES
, che si suppone essere
handicapées
Ecco un frammento di codice che mostra come sto usando il driver jackcess database a leggere nel file Acccess MDB in un ambiente Eclipse/Linux.
Database database = Database.open(new File(filepath));
Table table = database.getTable(tableName, true);
Iterator rowIter = table.iterator();
while (rowIter.hasNext()) {
Map<String, Object> row = this.rowIter.next();
// convert fields to UTF
Map<String, Object> rowUTF = new HashMap<String, Object>();
try {
for (String key : row.keySet()) {
Object o = row.get(key);
if (o != null) {
String valueCP850 = o.toString();
// String nameUTF8 = new String(valueCP850.getBytes("CP850"), "UTF8"); // does not work!
String valueISO = new String(valueCP850.getBytes("CP850"), "ISO-8859-1");
String valueUTF8 = new String(valueISO.getBytes(), "UTF-8"); // works!
rowUTF.put(key, valueUTF8);
}
}
} catch (UnsupportedEncodingException e) {
System.err.println("Encoding exception: " + e);
}
}
Nel codice vedrete dove voglio convertire direttamente in UTF-8, che non sembra funzionare, quindi devo fare una doppia conversione. Si noti inoltre che non sembra essere un modo per specificare il tipo di codifica quando si utilizza il driver jackcess.
Grazie, Cam
Questo non è UTF-8 ma piuttosto CP850. – Joey
Stai dicendo che la stringa originale è CP850? Mi rendo conto che la stringa originale non era UTF-8, anche se non ero sicuro di quale esatta codifica. È UTF-8 che sto cercando di convertirlo in modo che venga visualizzato correttamente. Ed è a mia conoscenza che il carattere É è supportato da UTF-8. Grazie. – cambo
'╔' è ciò che ottieni quando prendi' É' in CP1252 e lo interpreti come CP850. – Joey