文章详情

asp将中文汉字字符转为unicode编码与unicode编码转为汉字

2020/11/2 10:16:59    小编:胡金金个人网站

asp将中文汉字字符转为unicode编码也是非常常见的一种方式,尤其是转换以后,比如你在JS中解析的时候,如果不转换的话可能会出现乱码就是编码格式问题,如果是转换过的话,那么只需要JS在转换回来就不存在编码格式的问题,这个还是比较方便的,具体的可以看下后面的asp版的unicode转换方式!

<% dim zhuan_text
zhuan_text = "胡金金个人网站"
%>
要转换的内容:<%=zhuan_text%>
转换之后的代码:<%=tounicode(zhuan_text)%>
转码之后的文字:<%=unicodeto("\\u6df1\u5c71\u5de5\u4f5c\u5ba4")%>

<%
function tounicode(str) '中文转unicode
tounicode=""
dim i
for i=1 to len(str)
'asc函数:返回字符串的第一个字母对应的ANSI字符代码
'AscW函数:返回每一个GB编码文字的Unicode字符代码
'hex函数:返回表示十六进制数字值的字符串
tounicode=tounicode & "\u" & LCase(Right("0000" & Cstr(hex(AscW(mid(str,i,1)))),4))
next
end function

'\u6df1\u5c71\u5de5\u4f5c\u5ba4
function unicodeto(str) 'unicode转中文
str=replace(str,"\u","")
unicodeto=""
dim i
for i=1 to len(str) step 4
'cint函数:将Variant类型强制转换成int类型
'chr函数:返回数值对应的ANSI编码字符
'ChrW函数:返回数值对应的Unicode编码字符
unicodeto=unicodeto & ChrW(cint("&H" & mid(str,i,4)))
next
end function
%>

本文胡金金个人网站2020年11月2日整理发布

转载注明:http://m.hujinjin.com/info/17334.html

个人资讯推荐