native2ascii和ascii2native的JDK、JS、Java的几种实现方式

来源:互联网 发布:网络用词瘠薄什么意思 编辑:程序博客网 时间:2024/05/22 09:01
一、jdk命令代码
  1. native2ascii -encoding gb2312 native.properties > ascii.properties      
  2. native2ascii -reverse -encoding   gb2312 ascii.properties    > native2.properties 

    或者:

  1. native2ascii native.txt ascii.txt      
  2. native2ascii -reverse ascii.txt native2.txt

 

二、Javascript实现:

 

 

 

js 代码

  1. var keyStr = "ABCDEFGHIJKLMNOP" +       
  2.               "QRSTUVWXYZabcdef" +       
  3.               "ghijklmnopqrstuv" +       
  4.               "wxyz0123456789+/" +       
  5.               "=";       
  6.       
  7. function native2ascii(strNative) {       
  8.      var output = "";       
  9.      for (var i=0; i<strNative.length; i++) {       
  10.          var c = strNative.charAt(i);       
  11.          var cc = strNative.charCodeAt(i);       
  12.          if (cc > 0xff)       
  13.             output += "//u" + toHex(cc >> 8) + toHex(cc & 0xff);       
  14.          else       
  15.             output += c;       
  16.       }       
  17.      return output;       
  18. }       
  19.       
  20. var hexChars = "0123456789ABCDEF";       
  21. function toHex(n) {       
  22.      var nH = (n >> 4) & 0x0f;       
  23.      var nnL = n & 0x0f;       
  24.      return hexChars.charAt(nH) + hexChars.charAt(nL);       
  25. }       
  26.       
  27. function ascii2native(strAscii) {       
  28.      var output = "";       
  29.      var posFrom = 0;       
  30.      var posTo = strAscii.indexOf("//u", posFrom);       
  31.      while (posTo >= 0) {       
  32.           output += strAscii.substring(posFrom, posTo);       
  33.           output += toChar(strAscii.substr(posTo, 6));       
  34.           posFrom = posTo + 6;       
  35.           posTo = strAscii.indexOf("//u", posFrom);       
  36.       }       
  37.       output += strAscii.substr(posFrom);       
  38.      return output;       
  39. }       
  40.       
  41. function toChar(str) {       
  42.      if (str.substr(0, 2) != "//u") return str;       
  43.       
  44.      var code = 0;       
  45.      for (var i=2; i<str.length; i++) {       
  46.          var cc = str.charCodeAt(i);       
  47.          if (cc >= 0x30 && cc <= 0x39)       
  48.               cccc = cc - 0x30;       
  49.          else if (cc >= 0x41 && cc <= 0x5A)       
  50.               cccc = cc - 0x41 + 10;       
  51.          else if (cc >= 0x61 && cc <= 0x7A)       
  52.               cccc = cc - 0x61 + 10;       
  53.       
  54.           code <<= 4;       
  55.           code += cc;       
  56.       }       
  57.       
  58.      if (code < 0xff) return str;       
  59.       
  60.      return String.fromCharCode(code);       

 

 

 

  1. package util;   
  2.   
  3. /**  
  4.  * native2ascii.exe Java code implementation.  
  5.  *   
  6.  * @author  
  7.  * @version 1.0  
  8.  */  
  9. public class Native2AsciiUtils {   
  10.   
  11.     /**  
  12.      * prefix of ascii string of native character  
  13.      */  
  14.     private static String PREFIX = "//u";   
  15.   
  16.     /**  
  17.      * Native to ascii string. It's same as execut native2ascii.exe.  
  18.      *   
  19.      * @param str  
  20.      *            native string  
  21.      * @return ascii string  
  22.      */  
  23.     public static String native2Ascii(String str) {   
  24.         char[] chars = str.toCharArray();   
  25.         StringBuilder sb = new StringBuilder();   
  26.         for (int i = 0; i < chars.length; i++) {   
  27.             sb.append(char2Ascii(chars[i]));   
  28.         }   
  29.         return sb.toString();   
  30.     }   
  31.   
  32.     /**  
  33.      * Native character to ascii string.  
  34.      *   
  35.      * @param c  
  36.      *            native character  
  37.      * @return ascii string  
  38.      */  
  39.     private static String char2Ascii(char c) {   
  40.         if (c > 255) {   
  41.             StringBuilder sb = new StringBuilder();   
  42.             sb.append(PREFIX);   
  43.             int code = (c >> 8);   
  44.             String tmp = Integer.toHexString(code);   
  45.             if (tmp.length() == 1) {   
  46.                 sb.append("0");   

二、Java实现:

 

 

Java 代码

  1.             }   
  2.             sb.append(tmp);   
  3.             code = (c & 0xFF);   
  4.             tmp = Integer.toHexString(code);   
  5.             if (tmp.length() == 1) {   
  6.                 sb.append("0");   
  7.             }   
  8.             sb.append(tmp);   
  9.             return sb.toString();   
  10.         } else {   
  11.             return Character.toString(c);   
  12.         }   
  13.     }   
  14.   
  15.     /**  
  16.      * Ascii to native string. It's same as execut native2ascii.exe -reverse.  
  17.      *   
  18.      * @param str  
  19.      *            ascii string  
  20.      * @return native string  
  21.      */  
  22.     public static String ascii2Native(String str) {   
  23.         StringBuilder sb = new StringBuilder();   
  24.         int begin = 0;   
  25.         int index = str.indexOf(PREFIX);   
  26.         while (index != -1) {   
  27.             sb.append(str.substring(begin, index));   
  28.             sb.append(ascii2Char(str.substring(index, index + 6)));   
  29.             begin = index + 6;   
  30.             index = str.indexOf(PREFIX, begin);   
  31.         }   
  32.         sb.append(str.substring(begin));   
  33.         return sb.toString();   
  34.     }   
  35.   
  36.     /**  
  37.      * Ascii to native character.  
  38.      *   
  39.      * @param str  
  40.      *            ascii string  
  41.      * @return native character  
  42.      */  
  43.     private static char ascii2Char(String str) {   
  44.         if (str.length() != 6) {   
  45.             throw new IllegalArgumentException(   
  46.                     "Ascii string of a native character must be 6 character.");   
  47.         }   
  48.         if (!PREFIX.equals(str.substring(02))) {   
  49.             throw new IllegalArgumentException(   
  50.                     "Ascii string of a native character must start with /"//u/".");   
  51.         }   
  52.         String tmp = str.substring(24);   
  53.         int code = Integer.parseInt(tmp, 16) << 8;   
  54.         tmp = str.substring(46);   
  55.         code += Integer.parseInt(tmp, 16);   
  56.         return (char) code;   
  57.     }   
  58.   
  59. }