2 Replies Latest reply: Mar 26, 2007 7:04 PM by 807606 RSS

    How can to convert string in Unicode and vice versa

    807606
      Hi,
      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)
        • 1. Re: How can to convert string in Unicode and vice versa
          796365
          Java strings are are composed of Unicode characters, already.

          Please post a small (10 to 12 line) compilable program that shows what you what to do, and exhibits the problem.
          • 2. Re: How can to convert string in Unicode and vice versa
            807606
            Your question is difficult to understand. Are trying to do the same thing as the poster of this other thread?

            http://forum.java.sun.com/thread.jspa?threadID=5153150&tstart=0

            In any case, you should NEVER do this:
              sInitial = new String(sInitial.getBytes("UTF-8"), "UTF-8");
            You can't change the encoding of a string, ever. In this case, the resulting string will be exactly the same as the original because both of the encodings are the same. If you use two different encodings, you'll probably end up with garbage.