• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2115
  • Last Modified:

Convert Java Unicode \uXXXX to UTF8

How could i convert Java Unicode \uXXXX to UTF8? I tried the JAVA program as below but went into trouble. Could you help?

Or, is there conversion executable for download that can perform the job?



import java.io.* ;
import java.util.*;

public class Asc2Uni
{
      public static void main( String [] args )
      {
            
            File infile = null ;
            File outfile = null ;
            BufferedReader source = null ;
            FileOutputStream dest = null ;
            String infilename = null ;
            String outfilename = null ;
            try
            {
                  if (args.length<2)
                  {
                        System.err.println( "\nUsage: java Asc2Uni <source file> <destination file>\n" ) ;
                        return ;
                  }
                  infilename = args[0] ;
                  outfilename = args[1] ;
                  infile = new File( infilename ) ;
                  outfile = new File( outfilename ) ;
                  if (!infile.exists())
                  {
                        System.err.println( "Invalid input file" ) ;
                        return ;
                  }
                  else if (!infile.canRead())
                  {
                        System.err.println( "Can't read input file" ) ;
                        return ;
                  }
                  else if (outfile.isDirectory())
                  {
                        outfile = new File( outfile.getParent() + "\\" + infile.getName() ) ;
                        System.out.println( "Output file corrected to '" + outfile.getPath() + "'" ) ;
                  }
                  else if (!outfile.canWrite())
                  {
                        System.err.println( "Can't write output file" ) ;
                        return ;
                  }
                  else if (!infile.canRead())
                  {
                        System.err.println( "Can't read input file" ) ;
                        return ;
                  }
                  try
                  {
                        // The input source file contain the string \u00ea\u00f1
                        source = new BufferedReader( new FileReader( infile ) );
                        dest = new FileOutputStream( outfile ) ;
                        Writer out = new OutputStreamWriter(dest, "UTF8");
                        while( source.ready() ){
                          out.write( source.readLine() + "\r\n"); // Don't work!
                          out.write( "\u00ea\u00f1" + "\r\n"); // Get what I want
                        }
                 out.close();


                  }
                  finally
                  {
                        if (source!=null)
                        {
                              try
                              {
                                    source.close() ;
                              }
                              catch (IOException e)
                              {
                                    System.err.println( e.toString() ) ;
                              }
                        }
                                else if (dest!=null)
                        {
                              try
                              {
                                    dest.close() ;
                              }
                              catch (IOException e)
                              {
                                    System.err.println( e.toString() ) ;
                              }
                        }
                  }
            }
            catch (IOException e)
            {
                  System.err.println( e.toString() ) ;
            }
      }
}
0
kenchan2000
Asked:
kenchan2000
  • 2
1 Solution
 
CodingExpertsCommented:
try {
        // Convert from Unicode to UTF-8
        String string = "abc\u5639\u563b";
        byte[] utf8 = string.getBytes("UTF-8");
   
        // Convert from UTF-8 to Unicode
        string = new String(utf8, "UTF-8");
    } catch (UnsupportedEncodingException e) {
}

Hope this snippet helps...
-CE
0
 
kenchan2000Author Commented:
// Still don't work!
out.write( new String((source.readLine()).getBytes("UTF-8"), "UTF-8") + "\r\n");

// Works!
out.write( "\u00ea\u00f1" + "\r\n");

//source.readLine() returns "\u00ea\u00f1"
0
 
kenchan2000Author Commented:
Finally got the solution

native2ascii -reverse -encoding UTF8 input.txt output.txt
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now