package cn.cnnic.ops.udf;

public class GetChineseFromPunycode {

    static int TMIN = 1;
static int TMAX = 26;
static int BASE = 36;
static int INITIAL_N = 128;
static int INITIAL_BIAS = 72;
static int DAMP = 700;
static int SKEW = 38;
static char DELIMITER = '-';
static String PUNY_PREFIX = "xn--";
static char DOT = '.';
static String SPLIT_DOT = "\\."; public static void main(String[] args) {
String str = "xn--fiq7iz9az60bsyah94knxag3d.xn--fiqs8s";
GetChineseFromPunycode gpfc = new GetChineseFromPunycode();
System.out.println(gpfc.evaluate(str));
} public String evaluate(String txt) {
String strResult = txt.toString().trim();
try {
strResult = fromPunycodeToChinese(txt.toString().trim());
} catch (Exception e) {
e.printStackTrace();
}
return strResult;
} /**
*
* @param input
* @return
* @throws Exception
*/
public static String fromPunycodeToChineseUnit(String input) throws Exception {
int n = INITIAL_N;
int i = 0;
int bias = INITIAL_BIAS;
StringBuilder output = new StringBuilder();
int d = input.lastIndexOf(DELIMITER);
if (d > 0) {
for (int j = 0; j < d; j++) {
char c = input.charAt(j);
if (!isBasic(c)) {
throw new Exception("BAD_INPUT");
}
output.append(c);
}
d++;
} else {
d = 0;
}
while (d < input.length()) {
int oldi = i;
int w = 1;
for (int k = BASE;; k += BASE) {
if (d == input.length()) {
throw new Exception("BAD_INPUT");
}
int c = input.charAt(d++);
int digit = codepoint2digit(c);
if (digit > (Integer.MAX_VALUE - i) / w) {
throw new Exception("OVERFLOW");
}
i = i + digit * w;
int t;
if (k <= bias) {
t = TMIN;
} else if (k >= bias + TMAX) {
t = TMAX;
} else {
t = k - bias;
}
if (digit < t) {
break;
}
w = w * (BASE - t);
}
bias = adapt(i - oldi, output.length() + 1, oldi == 0);
if (i / (output.length() + 1) > Integer.MAX_VALUE - n) {
throw new Exception("OVERFLOW");
}
n = n + i / (output.length() + 1);
i = i % (output.length() + 1);
output.insert(i, (char) n);
i++;
}
return output.toString();
} /**
*
* @param delta
* @param numpoints
* @param first
* @return
*/
public static int adapt(int delta, int numpoints, boolean first) {
if (first) {
delta = delta / DAMP;
} else {
delta = delta / 2;
}
delta = delta + (delta / numpoints);
int k = 0;
while (delta > ((BASE - TMIN) * TMAX) / 2) {
delta = delta / (BASE - TMIN);
k = k + BASE;
}
return k + ((BASE - TMIN + 1) * delta) / (delta + SKEW);
} /**
*
* @param c
* @return
*/
public static boolean isBasic(char c) {
return c < 0x80;
} /**
*
* @param d
* @return
* @throws Exception
*/
public static int digit2codepoint(int d) throws Exception {
if (d < 26) {
// 0..25 : 'a'..'z'
return d + 'a';
} else if (d < 36) {
// 26..35 : '0'..'9';
return d - 26 + '0';
} else {
throw new Exception("BAD_INPUT");
}
} /**
*
* @param c
* @return
* @throws Exception
*/
public static int codepoint2digit(int c) throws Exception {
if (c - '0' < 10) {
// '0'..'9' : 26..35
return c - '0' + 26;
} else if (c - 'a' < 26) {
// 'a'..'z' : 0..25
return c - 'a';
} else {
throw new Exception("BAD_INPUT");
}
} /**
*
* @param input
* @return
* @throws Exception
*/
public static String fromPunycodeToChinese(String input) throws Exception {
if (input == null || input.equalsIgnoreCase("")) {
return "";
} else if (input.indexOf(DOT) < 0) {
if (input.startsWith(PUNY_PREFIX)) {
return fromPunycodeToChineseUnit(input.substring(PUNY_PREFIX.length()));
} else {
return input;
} } else if (input.indexOf(DOT) > 0) {
String[] arr = input.split(SPLIT_DOT);
String result = "";
for (int index = 0; index < arr.length; index++) {
if (arr[index].startsWith(PUNY_PREFIX)) {
result += fromPunycodeToChineseUnit(arr[index].substring(PUNY_PREFIX.length())) + ".";
} else {
result += arr[index] + ".";
}
}
return result.substring(0, result.length() - 1);
}
return input;
}
}

【参考】http://blog.csdn.net/a19881029/article/details/18262671

Punycode转中文的更多相关文章

  1. Punycode与中文互转

    Punycode是一个根据RFC 3492标准而制定的编码系统,主要用于把域名从地方语言所采用的Unicode编码转换成为可用于DNS系统的编码 "中文域名"不被标准的解析服务器支 ...

  2. 如何实现Punycode中文域名转码

    如果你见过中文域名应该会觉得很奇怪,为什么复制出来的域名变成一个很莫名其妙的字符串,比如这个秀恩爱的域名“郝越.我爱你”,实际显示的域名是 http://xn--vq3al9d.xn--6qq986b ...

  3. 中文转Punycode

    package cn.cnnic.ops.udf; public class GetPunycodeFromChinese { static int TMIN = 1; static int TMAX ...

  4. Python中文乱码

    1,注意:请使用智慧型浏览器 "CHROME" 配合理解和运作本文中提到的程序. 2,提示:谷歌的CHROME浏览器是迄今为止最智慧的浏览器,没有之一,只有第一. 3,谷歌的CHR ...

  5. Nginx中文域名配置

    Nginx虚拟主机上绑定一个带中文域名,比如linuxeye.中国,浏览器不能跳转. why? 因为操作系统的核心都是英文组成,DNS服务器的解析也是由英文代码交换,所以DNS服务器上并不支持直接的中 ...

  6. apache支持中文域名绑定,apache支持中文域名绑定,教你怎样让apache支持中文域名绑定

    摘要:apache支持中文域名绑定,apache支持中文域名绑定,教你怎样让apache支持中文域名绑定,根据本人实际经验,叫你如何让apache支持中文域名绑定,绝对管用的让apache支持中文域名 ...

  7. nginx配置中文域名解析

    当nginx配置文件中的default如果遇到解析指向问题的时候 ,配置了中文 没有用 后来找了找这个网址 http://tools.jb51.net/punycode/ 然后进去转换了一下 把 评估 ...

  8. CNAME关联githubPage域名及中文域名,创建个人网站

    对于前端开发来说,部署一个自己的个人网站部署服务器等比较麻烦,如果只是做静态页面的展示GitHubPage完全够用,而且有300M免费的空间,完全满足需求. 首先你要有GitHubPage项目,具体怎 ...

  9. Punycode

    Punycode是一个根据RFC 3492标准而制定的编码系统,主要用于把域名从地方语言所采用的Unicode编码转换成为可用于DNS系统的编码 “中文域名”不被标准的解析服务器支持,需转化为Puny ...

随机推荐

  1. OpenERP在product中增加外部网络链接图片

    最近的一个项目要求在Product_Template中增加类似与HTML中<img src=”" />的形式的图片 product_img_extra.py from osv i ...

  2. VC2012编译CEF3-转

    原文地址:http://blog.csdn.net/tiplip/article/details/42047815 下载 代码下载:http://cefbuilds.com/,CEF 3.2556.1 ...

  3. 腾讯云-搭建 Python 开发环境

    搭建 Python 开发环境 准备工作 任务时间:5min ~ 10min Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.首先我们来看看系统中是否已经存在 Python ,并安装 ...

  4. PmException--- SQL(统计报表)

    select TAGS,GROUP_CONCAT(TAGS) t from EXCEPTION_RESULT e,PM_TASK t ' and t.OWNER='admin' group by TA ...

  5. PHP中一些有用的函数

    <?php /** * 加密解密 * * @param string $key * @param string $string * @param string $decrypt * @retur ...

  6. ArchLinux安装 LXDE

    http://wiki.lxde.org/zh/index.php?title=ArchLinux&variant=zh-cn 透过 pacman 安装 LXDE 大多数的最新 LXDE 套件 ...

  7. c# 网站生成静态页面

    在一些需要经常更新页面数据的网站中,一般访问量不是很大的都直接发布的是带后台代码,每次访问都是有数据库交互的.但是一旦访问量增加了,那么这些服务器开销变成本就要考虑进来了,像一些文章,后台编辑后,文章 ...

  8. Java Web Project自定义错误页面,log4j记录日志。

    创建记录日志的文件LoggerHelper.java: package com.wyp.helper; import org.apache.log4j.Logger; public class Log ...

  9. Kafka中Topic级别配置

    一.Kafka中topic级别配置 1.Topic级别配置 配置topic级别参数时,相同(参数)属性topic级别会覆盖全局的,否则默认为全局配置属性值. 创建topic参数可以设置一个或多个--c ...

  10. SSAS 笔记

    SQL SERVER 2000创建多维数据集对应的cub文件   http://blog.csdn.net/zklth/article/details/6367816     http://msdn. ...