ASCII 2 NATIVE : Code Unicode « Development Class « Java






ASCII 2 NATIVE

     
/**
 * 
 */
//package com.exedosoft.plat.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * @author Administrator
 * 
 */
public class ASCII2NATIVE {

  /**
   * 
   */
  public ASCII2NATIVE() {
    // TODO Auto-generated constructor stub
  }

  public static void main(String[] args) {
    File f = new File(
        "c:\\mydb.script");
    File f2 = new File(
    "c:\\mydb3.script");
    if (f.exists() && f.isFile()) {
      // convert param-file
      BufferedReader br = null;
      StringBuffer sb = new StringBuffer();
      String  line;

      try {
        br = new BufferedReader(new InputStreamReader(
            new FileInputStream(f), "JISAutoDetect"));

        while ((line = br.readLine()) != null) {
          System.out.println(ascii2Native(line));
          sb.append(ascii2Native(line)).append(";\n");//.append(";\n\r")
        }
        
        
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new  FileOutputStream(f2),"utf-8"));
        out.append(sb.toString());
        out.flush();
        out.close();
      } catch (FileNotFoundException e) {
        System.err.println("file not found - " + f);
      } catch (IOException e) {
        System.err.println("read error - " + f);
      } finally {
        try {
          if (br != null)
            br.close();
        } catch (Exception e) {
        }
      }
    } else {
      // // convert param-data
      // System.out.print(ascii2native(args[i]));
      // if (i + 1 < args.length)
      // System.out.print(' ');
    }
  }

  /**    
     * prefix of ascii string of native character    
     */     
    private static String PREFIX = "\\u";      
     
    /**    
     * Native to ascii string. It's same as execut native2ascii.exe.    
     *     
     * @param str    
     *            native string    
     * @return ascii string    
     */     
    public static String native2Ascii(String str) {      
        char[] chars = str.toCharArray();      
        StringBuilder sb = new StringBuilder();      
        for (int i = 0; i < chars.length; i++) {      
            sb.append(char2Ascii(chars[i]));      
        }      
        return sb.toString();      
    }      
     
    /**    
     * Native character to ascii string.    
     *     
     * @param c    
     *            native character    
     * @return ascii string    
     */     
    private static String char2Ascii(char c) {      
        if (c > 255) {      
            StringBuilder sb = new StringBuilder();      
            sb.append(PREFIX);      
            int code = (c >> 8);      
            String tmp = Integer.toHexString(code);      
            if (tmp.length() == 1) {      
                sb.append("0");      
            }      
            sb.append(tmp);      
            code = (c & 0xFF);      
            tmp = Integer.toHexString(code);      
            if (tmp.length() == 1) {      
                sb.append("0");      
            }      
            sb.append(tmp);      
            return sb.toString();      
        } else {      
            return Character.toString(c);      
        }      
    }      
     
    /**    
     * Ascii to native string. It's same as execut native2ascii.exe -reverse.    
     *     
     * @param str    
     *            ascii string    
     * @return native string    
     */     
    public static String ascii2Native(String str) {      
        StringBuilder sb = new StringBuilder();      
        int begin = 0;      
        int index = str.indexOf(PREFIX);      
        while (index != -1) {      
            sb.append(str.substring(begin, index));      
            sb.append(ascii2Char(str.substring(index, index + 6)));      
            begin = index + 6;      
            index = str.indexOf(PREFIX, begin);      
        }      
        sb.append(str.substring(begin));      
        return sb.toString();      
    }      
     
    /**    
     * Ascii to native character.    
     *     
     * @param str    
     *            ascii string    
     * @return native character    
     */     
    private static char ascii2Char(String str) {      
        if (str.length() != 6) {      
            throw new IllegalArgumentException(      
                    "Ascii string of a native character must be 6 character.");      
        }      
        if (!PREFIX.equals(str.substring(0, 2))) {      
            throw new IllegalArgumentException(      
                    "Ascii string of a native character must start with \"\\u\".");      
        }      
        String tmp = str.substring(2, 4);      
        int code = Integer.parseInt(tmp, 16) << 8;      
        tmp = str.substring(4, 6);      
        code += Integer.parseInt(tmp, 16);      
        return (char) code;      
    }      


}

   
    
    
    
    
  








Related examples in the same category

1.Unicode sortingUnicode sorting
2.Unicode: Fonts and Text RenderingUnicode: Fonts and Text Rendering
3.Unicode: TrueType Font TestUnicode: TrueType Font Test
4.Unicode: test layoutUnicode: test layout
5.Conversion between Unicode characters and StringsConversion between Unicode characters and Strings
6.Convert among Unicode, ASCII and byte/intConvert among Unicode, ASCII and byte/int
7.Unicode - show a page of Unicode characters
8.Return the Unicode char which is coded in the bytes at the given position.
9.Vis - make special characters visible
10.Demonstrate creating readers and writers with a specific encoding
11.TextArea with UnicodeTextArea with Unicode
12.Unicode displayUnicode display
13.Check if the current character is an 7 bits ASCII CHAR (between 0 and 127). <char> ::= <alpha> | <digit> | '-'
14.Internationalized Graphical User Interfaces: unicode cut and pasteInternationalized Graphical User Interfaces: unicode cut and paste
15.Unicode to ascii
16.Return the Unicode char which is coded in the bytes at position 0.
17.Count the number of bytes needed to return an Unicode char. This can be from 1 to 6.
18.Return the number of bytes that hold an Unicode char.
19.An optimized reader for reading byte streams that only contain 7-bit ASCII characters.
20.Stream Converter UnicodeStream Converter Unicode
21.String Converter UnicodeString Converter Unicode
22.Checks if the String contains only unicode digits or space
23.Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.
24.Checks if the String contains only unicode letters and space (' ').
25.Checks if the String contains only unicode letters or digits.
26.Checks if the String contains only unicode letters, digits or space (' ').
27.Checks if the String contains only unicode letters.
28.Unicode Util
29.Unicode reader
30.Get UTF String Size