主要用于生成json格式时,将汉字转成Unicoude编码,防止页面乱码。
PRotected string GetUnicode(string text) { string result = ""; for (int i = 0; i < text.Length; i++) { if ((int)text[i] > 32 && (int)text[i] < 127) { result += text[i].ToString(); } else result += string.Format("//u{0:x4}", (int)text[i]); } return result; }
运行结果:
涉及的知识点:
1、“x”则代表十六进制,“x4”代表十六进制表示的可控制长度,如果长度不够,则用前导的0填补。
2、Unicode写法:在表示一个Unicode的字符时,通常会用“U+”然后紧接着一组十六进制的数字来表示这一个字符。
3、ASCII 码(American Standard Code for Information Interchange,全称美国信息交换标准码)
基本的 ASCII字符集共有 128 个字符,其中有 96 个可打印字符,包括常用的字母、数字、标点符号等,另外还有 32 个控制字符。
参考资料:
1、C# String.Format用法和格式说明
http://www.VEVb.com/gongth-12/archive/2012/11/26/2791642.html#undefined
新闻热点
疑难解答