How can to convert string in Unicode and vice versa
807606Mar 26 2007 — edited Mar 26 2007Hi,
How can to convert string in Unicode and vice versa
Frenchhindi.txt
��
siddharth singh
सुधर भी जाओ वरना तकलिफ़ होगी भारी
जन हित मे जारी।
abc
I want to read a file that contains French/Hindi(Non English character) Say French.txt
and have to convert that in file that contains Unicode of that. Say it
FrenchUnicode.txt
I have to read FrenchUnicode.txt and then have to again convert this unicode charcter to again French. say (French1.txt);
Please help me.
I am using this code to generate unicode
public static void readFile(String file){
File f= new File(file);
StringBuffer sb = new StringBuffer();
try {
InputStreamReader ir = new InputStreamReader(new FileInputStream(f),"UTF-8");
int charint=0;
String sInitial="";
byte bRay[] = null;
try {
while((charint=ir.read())!= -1){
sInitial =" "+(char)charint;
debug(sInitial);
try
{
sInitial = new String( sInitial.getBytes("UTF-8"), "UTF-8");
bRay = sInitial.getBytes("UTF-8");
}
catch( UnsupportedEncodingException uee )
{
System.out.println( "Exception: " + uee);
}
for( int ndx = 0; ndx < bRay.length; ndx++ )
{
System.out.print( Integer.toHexString( bRay[ ndx ] ) + " " );
sb.append(Integer.toHexString( bRay[ ndx ] ));
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print(("unicode way of string ="+sb);
}
but the generated code i snot able to again convert in french/hindi(non engligh character )
public static String convertToUnicodeString(String hexString)
{
debug("hexString="+hexString+" l="+hexString.length());
StringBuffer output = new StringBuffer();
String subStr = null;
debug("\n\n");
for (int i =0 ; i < hexString.length() ;i=i+2 )
{
subStr = hexString.substring(i,i+2);
char c = (char) Integer.parseInt(subStr, 16);
debug(""+c);
output.append(c);
}
return output.toString();
}
Siddharth Singh(singhsiddh@yahoo.com)