Native2Ascii和Ascii2Native的Java实现(中文转ascii)

/**    
 * 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();      
        StringBuffer sb = new StringBuffer();      
        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) {      
        	StringBuffer sb = new StringBuffer();      
            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) {      
    	StringBuffer sb = new StringBuffer();      
        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;      
    }      
     
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值