Sunday, January 13, 2013

Character Encoding

                                         CONVERT CHARACTER ENCODING
Lots of times we encounter character problems such as why Chinese characters are square and Arabic characters are ?. To achive this problem i was using unicode converters and i showed the characters as their unicode codes. So how can we convert characters from Utf to Iso or to Unicode there are small functions below :

1 ) If you want to convert your utf-8 data to unicode this function may help you


private String convertConvertingToUnicode(String str) {
// TODO Auto-generated method stub
unicodeString unicode = new unicodeString();
String converted = unicude.convert(str);
return converted;

}


2 ) If you want to convert unicode to Utf this may help you

String convertConvertingToUnicode =<--//Your string that contains unicode code
String[] strs = convertConvertingToUnicode.split("\\\\");
String result = "";
for(int i = 0;i < strs.length;i++)
{
String send = strs[i];
if(strs[i].length()>0)
if(strs[i].charAt(0) == 'u'){
send = "\\"+strs[i];
}
result += convertToUtf(send);
}

//All the document now is in result as utf with meaningfull
}

private String convertToUtf(String string) {
// TODO Auto-generated method stub
String str = string;
Matcher m = Pattern.compile("(?i)\\\\u([\\da-f]{4})").matcher(str);
String a = str;
if (m.find()) {
   a = String.valueOf((char) Integer.parseInt(m.group(1), 16));
}
if(str.length() > 6)
{
a += str.substring(6);
}
if(a.compareTo("")==0){
a = str;
}
return a;
}

3 ) And also you can change the encoding type

byte[] byts = string.getBytes("UTF-8");
String newstring= new String(byts,"Whatever you want for example  ISO-8859-9");

I needed a converter lots of time in development times so i prepared a program on android that may also help you for the character conversion you can try from your android device...

If you are getting unreadable words; if you try in eclipse ide you should look at the console encoding that is in runconfigurations or if you are developing ee in JSP-JF-SERVLET-STRUTS-SPRING... any web based technology, you should look first to the page encodings, then tomcat startup parameters...
Android Application



No comments:

Post a Comment