一、jdk命令代码
- native2ascii -encoding gb2312 native.properties > ascii.properties
- native2ascii -reverse -encoding gb2312 ascii.properties > native2.properties
或者:
- native2ascii native.txt ascii.txt
- native2ascii -reverse ascii.txt native2.txt
二、Javascript实现:
js 代码
- var keyStr = "ABCDEFGHIJKLMNOP" +
- "QRSTUVWXYZabcdef" +
- "ghijklmnopqrstuv" +
- "wxyz0123456789+/" +
- "=";
- function native2ascii(strNative) {
- var output = "";
- for (var i=0; i<strNative.length; i++) {
- var c = strNative.charAt(i);
- var cc = strNative.charCodeAt(i);
- if (cc > 0xff)
- output += "//u" + toHex(cc >> 8) + toHex(cc & 0xff);
- else
- output += c;
- }
- return output;
- }
- var hexChars = "0123456789ABCDEF";
- function toHex(n) {
- var nH = (n >> 4) & 0x0f;
- var nnL = n & 0x0f;
- return hexChars.charAt(nH) + hexChars.charAt(nL);
- }
- function ascii2native(strAscii) {
- var output = "";
- var posFrom = 0;
- var posTo = strAscii.indexOf("//u", posFrom);
- while (posTo >= 0) {
- output += strAscii.substring(posFrom, posTo);
- output += toChar(strAscii.substr(posTo, 6));
- posFrom = posTo + 6;
- posTo = strAscii.indexOf("//u", posFrom);
- }
- output += strAscii.substr(posFrom);
- return output;
- }
- function toChar(str) {
- if (str.substr(0, 2) != "//u") return str;
- var code = 0;
- for (var i=2; i<str.length; i++) {
- var cc = str.charCodeAt(i);
- if (cc >= 0x30 && cc <= 0x39)
- cccc = cc - 0x30;
- else if (cc >= 0x41 && cc <= 0x5A)
- cccc = cc - 0x41 + 10;
- else if (cc >= 0x61 && cc <= 0x7A)
- cccc = cc - 0x61 + 10;
- code <<= 4;
- code += cc;
- }
- if (code < 0xff) return str;
- return String.fromCharCode(code);
- }
-
- package util;
- /**
- * native2ascii.exe Java code implementation.
- *
- * @author
- * @version 1.0
- */
- public class Native2AsciiUtils {
- /**
- * 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");
二、Java实现:
Java 代码
- }
- 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;
- }
- }