Windows Arabic Encoding Cp1256 &#XXX;

in this post I will address the Arabic encoding problems.

any one may face one or more of the following scenarios:

  1. Retrieving Arabic data from database and wanna display it in a web page correctly.­
  2. Getting encoded Arabic data from web page and reading it correctly.

for the first Scenario  Retrieving Arabic data from database and wanna display it in a web page correctly.

it may works great with you by just adding the following page Encoding to your jsp page
­

­<%@ page pageEncoding="windows-1256" %>

and if it didn't work you may need to do encode the retrieved Arabic String as follows:

 String value2 = new String(arabicString.getBytes(), "Cp1256");

and this will encode the Arabic letters as &#XXX;:

 for example "المستخدم" word will be translated to "&#1575;&#1604;&#1605;&#1587;&#1578;&#1582;&#1583;&#1605;" inside the HTML page.

for the second Scenario Getting encoded Arabic data from web page and reading it correctly.

you will need to manipulate the retrieved Arabic encoded serious ­"&#1575;&#1604;&#1605;&#1587;&#1578;&#1582;&#1583;&#1605;" as follows: 

 ­/**     
  * decode th­e passed value
  * @return the encoded arabic value
  * @param value
  */­
private String denodeToArabic(String value) {      
   if(value.indexOf("&#")==-1)
     return value;
      
   String newString ="";
   value = value.replaceAll("&#","");
   String[] characters = value.split(";");
   for(int i=0; i<characters.length; i++){
      if(characters[i].startsWith(" "))
         newString +=" ";
      else if(characters[i].trim().length()!=4)
         newString += characters[i].trim();
      else
         newString +=(char)Integer.valueOf(characters[i].trim()).intValue()+"";      ­
   }
      
   return newString;
}­­

 References:

  • ­
    http://download.oracle.com/docs/cd/B10500_01/server.920/a96529/appa.htm#958278
  • http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/native2ascii.html

Powered by Drupal - Design by artinet