Punycode与中文互转
其实目前所说的各种浏览器完美支持中文域名,只是浏览器中主动加入了中文域名自动转码,不需要再次安装中文域名转码控件来完成整个流程
如在浏览器中输入"北京大学.com”,然后通过wireshark抓包
GET http://xn--1lq90ic7fzpc.com/ HTTP/1.1
public class CharsetTool {
	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 = '-';
	/**
	 * Punycodes a unicode string.
	 *
	 * @param input
	 *            Unicode string.
	 *
	 * @return Punycoded string.
	 */
	public static String encode(String input) throws Exception {
		int n = INITIAL_N;
		int delta = 0;
		int bias = INITIAL_BIAS;
		StringBuilder output = new StringBuilder();
		// Copy all basic code points to the output
		int b = 0;
		for (int i = 0; i < input.length(); i++) {
			char c = input.charAt(i);
			if (isBasic(c)) {
				output.append(c);
				b++;
			}
		}
		// Append delimiter
		if (b > 0) {
			output.append(DELIMITER);
		}
		int h = b;
		while (h < input.length()) {
			int m = Integer.MAX_VALUE;
			// Find the minimum code point >= n
			for (int i = 0; i < input.length(); i++) {
				int c = input.charAt(i);
				if (c >= n && c < m) {
					m = c;
				}
			}
			if (m - n > (Integer.MAX_VALUE - delta) / (h + 1)) {
				throw new Exception("OVERFLOW");
			}
			delta = delta + (m - n) * (h + 1);
			n = m;
			for (int j = 0; j < input.length(); j++) {
				int c = input.charAt(j);
				if (c < n) {
					delta++;
					if (0 == delta) {
						throw new Exception("OVERFLOW");
					}
				}
				if (c == n) {
					int q = delta;
					for (int k = BASE;; k += BASE) {
						int t;
						if (k <= bias) {
							t = TMIN;
						} else if (k >= bias + TMAX) {
							t = TMAX;
						} else {
							t = k - bias;
						}
						if (q < t) {
							break;
						}
						output.append((char) digit2codepoint(t + (q - t)
								% (BASE - t)));
						q = (q - t) / (BASE - t);
					}
					output.append((char) digit2codepoint(q));
					bias = adapt(delta, h + 1, h == b);
					delta = 0;
					h++;
				}
			}
			delta++;
			n++;
		}
		return output.toString();
	}
	/**
	 * Decode a punycoded string.
	 *
	 * @param input
	 *            Punycode string
	 *
	 * @return Unicode string.
	 */
	public static String decode(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();
	}
	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);
	}
	public static boolean isBasic(char c) {
		return c < 0x80;
	}
	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");
		}
	}
	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");
		}
	}
	public static void main(String[] args) throws Exception {
		 String strPunycode ="xn--"+ CharsetTool.encode("北京大学");
		 System.out.println(strPunycode);
		 String strChinese = CharsetTool.decode("1lq90ic7fzpc");
		 System.out.println(strChinese);
	}
}
xn--1lq90ic7fzpc
北京大学
Punycode与中文互转的更多相关文章
- Unicode编码与中文互转
		/** * unicode编码转换为汉字 * @param unicodeStr 待转化的编码 * @return 返回转化后的汉子 */ public static String UnicodeTo ... 
- c#unicode,中文互转
		/// <summary> /// 中文转unicode /// </summary> /// <returns></returns> public s ... 
- Punycode转中文
		package cn.cnnic.ops.udf; public class GetChineseFromPunycode { static int TMIN = 1; static int TMAX ... 
- JS 实现 unicode 中文互转
		// 转为unicode 编码 function encodeUnicode(str) { var res = []; for ( var i=0; i<str.length; i++ ) { ... 
- RGB与16进制色互转
		点击进入新版 <前端在线工具站> CSS, JavaScript 压缩YUI compressor, JSPacker...HTML特殊符号对照表PNG,GIF,JPG... Base ... 
- 个人永久性免费-Excel催化剂功能第29波-追加中国特色的中文相关自定义函数
		中文世界里,有那么几个需求在原生Excel里没提供,例如财务部的数字转大写金额,文字转拼音等,在其他插件里,大部分是以功能区菜单按钮的方式提供.Excel催化剂认为,最佳的使用方式乃是自定义函数的方式 ... 
- C#设计模式总结
		一.引言 经过这段时间对设计模式的学习,自己的感触还是很多的,因为我现在在写代码的时候,经常会想想这里能不能用什么设计模式来进行重构.所以,学完设计模式之后,感觉它会慢慢地影响到你写代码的思维方式.这 ... 
- jni数据传递——会不断的更新,测试没有问题,再整理进来。
		工作中遇到了ndk编程,其实核心就是java和本地的数据交互.现把所有数据类型的传递写成demo. 1,ini数组传递 我们实现传递8个数值过去,然后本地将八个数值放到数组,返回. java代码: ... 
- C#设计模式总结(转)
		一.引言 经过这段时间对设计模式的学习,自己的感触还是很多的,因为我现在在写代码的时候,经常会想想这里能不能用什么设计模式来进行重构.所以,学完设计模式之后,感觉它会慢慢地影响到你写代码的思维方式.这 ... 
随机推荐
- C#排序算法的比较
			首先通过图表比较不同排序算法的时间复杂度和稳定性. 排序方法 平均时间 最坏情况 最好情况 辅助空间 稳定性 直接插入排序 O(n2) O(n2) O(n) O(1) 是 冒泡排序 O(n2) O(n ... 
- Apache ‘mod_pagespeed’模块跨站脚本漏洞
			漏洞名称: Apache ‘mod_pagespeed’模块跨站脚本漏洞 CNNVD编号: CNNVD-201310-677 发布时间: 2013-11-05 更新时间: 2013-11-05 危害等 ... 
- 【转】Android版本和API Level对应关系
			原文网址:http://blog.csdn.net/huiguixian/article/details/39928027 从Android developer copy过来,留作笔记的. Platf ... 
- 【转】“/usr/bin/ld: cannot find -lz”
			原文网址:http://stackoverflow.com/questions/3373995/usr-bin-ld-cannot-find-lz I am trying to compile And ... 
- Go Hello World!
			有些事应该坚持去做 当你半途而废的时候意味着你又要重新开始.那么 Golang Hello world! Java Android 新手 学习 Golang First Day ! go 语言下载: ... 
- haproxy配置直接重定向url
			在邮件列表看到有个人问haproxy能否在接到一个请求时选择一个后端服务器,然后301重定向url .主要原因是他有5个1G的出口,这样就能充分利用其带宽.测试了一下是可以的 frontend fre ... 
- SQL经典题-实战
			Student(S#,Sname,Sage,Ssex) 学生表 S#:学号:Sname:学生姓名:Sage:学生年龄:Ssex:学生性别 Course(C#,Cname,T#) 课程表 ... 
- 关于 Unity 中 ModelImporter.optimizeGameObjects
			ModelImporter.optimizeGameObjects 能够优化骨骼动画,将无用的骨骼合并,效率测试官方给出的数据差距比较大( Unity CJ 干货分享:全新的Unity移动游 ... 
- asp.net mvc3+EF4.1项目实战
			ASP.NET身份验证机制membership入门——配置篇(1) http://www.cnblogs.com/xlb2000/archive/2010/05/10/1729076.html 1.添 ... 
- Oracle的dmp文件的导入
			项目开始拿到了dmp文件,数据库用的是10g的,但是尽然没导成功,后来想可能导出的时候用11导出的,决定试一下. 正好自己的机器是11的客户端,结果不识别imp命令,到安装目录下的bin文件夹下看尽然 ... 
