Created
March 1, 2012 20:48
-
-
Save luis-fss/1953132 to your computer and use it in GitHub Desktop.
Convertendo de UTF-8 para ISO-8859-1 em Java: A solução definitiva
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Recentemente enfrentei problemas em um aplicativo Android que desenvolvi, o qual se comunica com o banco de dados de um dos sistemas da empresa, codificado em ISO-8859-1 (Firebird) através de um web service. | |
Os dados eram gravados de forma errada, muitas vezes truncavam e as vezes apareciam caracteres estranhos. | |
Depois de algumas tentativas, cheguei até a seguinte solução: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String convertUTF8toISO(String str) { | |
String ret = null; | |
try { | |
ret = new String(str.getBytes("ISO-8859-1"), "UTF-8"); | |
} | |
catch (java.io.UnsupportedEncodingException e) { | |
return null; | |
} | |
return ret; | |
} |
Excelente conversor, foi muito útil! Muito obrigado!
Muito obrigado, funcionou muito bem pra min!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quando você faz "str.getBytes()", você faz uma conversão implícita baseada no charset do ambiente. Sua solucão é aparentemente dependente do seu ambiente e pode dar erro em outro ambiente.
Não testei, mas aparentemente você deveria fazer algo como 'str.getBytes("UTF-8")', ou algo assim.