Discuz教程网

ASP关于编码的几个有用的函数小结(utf8)

[复制链接]
authicon dly 发表于 2011-9-8 20:09:20 | 显示全部楼层 |阅读模式
1、'UTF转GB---将UTF8编码文字转换为GB编码文字
  1. function UTF2GB(UTFStr)

  2. for Dig=1 to len(UTFStr)
  3. '如果UTF8编码文字以%开头则进行转换
  4. if mid(UTFStr,Dig,1)="%" then
  5. 'UTF8编码文字大于8则转换为汉字
  6. if len(UTFStr) >= Dig+8 then
  7. GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9))
  8. Dig=Dig+8
  9. else
  10. GBStr=GBStr & mid(UTFStr,Dig,1)
  11. end if
  12. else
  13. GBStr=GBStr & mid(UTFStr,Dig,1)
  14. end if
  15. next
  16. UTF2GB=GBStr
  17. end function

  18. 'UTF8编码文字将转换为汉字
  19. function ConvChinese(x)
  20. A=split(mid(x,2),"%")
  21. i=0
  22. j=0
  23. for i=0 to ubound(A)
  24. A(i)=c16to2(A(i))
  25. next
  26. for i=0 to ubound(A)-1
  27. DigS=instr(A(i),"0")
  28. Unicode=""
  29. for j=1 to DigS-1
  30. if j=1 then
  31. A(i)=right(A(i),len(A(i))-DigS)
  32. Unicode=Unicode & A(i)
  33. else
  34. i=i+1
  35. A(i)=right(A(i),len(A(i))-2)
  36. Unicode=Unicode & A(i)
  37. end if
  38. next

  39. if len(c2to16(Unicode))=4 then
  40. ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode)))
  41. else
  42. ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode)))
  43. end if
  44. next
  45. end function

  46. '二进制代码转换为十六进制代码
  47. function c2to16(x)
  48. i=1
  49. for i=1 to len(x) step 4
  50. c2to16=c2to16 & hex(c2to10(mid(x,i,4)))
  51. next
  52. end function

  53. '二进制代码转换为十进制代码
  54. function c2to10(x)
  55. c2to10=0
  56. if x="0" then exit function
  57. i=0
  58. for i= 0 to len(x) -1
  59. if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i)
  60. next
  61. end function

  62. '十六进制代码转换为二进制代码
  63. function c16to2(x)
  64. i=0
  65. for i=1 to len(trim(x))
  66. tempstr= c10to2(cint(int("&h" & mid(x,i,1))))
  67. do while len(tempstr)<4
  68. tempstr="0" & tempstr
  69. loop
  70. c16to2=c16to2 & tempstr
  71. next
  72. end function

  73. '十进制代码转换为二进制代码
  74. function c10to2(x)
  75. mysign=sgn(x)
  76. x=abs(x)
  77. DigS=1
  78. do
  79. if x<2^DigS then
  80. exit do
  81. else
  82. DigS=DigS+1
  83. end if
  84. loop
  85. tempnum=x

  86. i=0
  87. for i=DigS to 1 step-1
  88. if tempnum>=2^(i-1) then
  89. tempnum=tempnum-2^(i-1)
  90. c10to2=c10to2 & "1"
  91. else
  92. c10to2=c10to2 & "0"
  93. end if
  94. next
  95. if mysign=-1 then c10to2="-" & c10to2
  96. end function
复制代码

2、'GB转UTF8--将GB编码文字转换为UTF8编码文字
  1. Function toUTF8(szInput)
  2. Dim wch, uch, szRet
  3. Dim x
  4. Dim nAsc, nAsc2, nAsc3
  5. '如果输入参数为空,则退出函数
  6. If szInput = "" Then
  7. toUTF8 = szInput
  8. Exit Function
  9. End If
  10. '开始转换
  11. For x = 1 To Len(szInput)
  12. '利用mid函数分拆GB编码文字
  13. wch = Mid(szInput, x, 1)
  14. '利用ascW函数返回每一个GB编码文字的Unicode字符代码
  15. '注:asc函数返回的是ANSI 字符代码,注意区别
  16. nAsc = AscW(wch)
  17. If nAsc < 0 Then nAsc = nAsc + 65536

  18. If (nAsc And &HFF80) = 0 Then
  19. szRet = szRet & wch
  20. Else
  21. If (nAsc And &HF000) = 0 Then
  22. uch = "%" & Hex(((nAsc \ 2 ^ 6)) Or &HC0) & Hex(nAsc And &H3F Or &H80)
  23. szRet = szRet & uch
  24. Else
  25. 'GB编码文字的Unicode字符代码在0800 - FFFF之间采用三字节模版
  26. uch = "%" & Hex((nAsc \ 2 ^ 12) Or &HE0) & "%" & _
  27. Hex((nAsc \ 2 ^ 6) And &H3F Or &H80) & "%" & _
  28. Hex(nAsc And &H3F Or &H80)
  29. szRet = szRet & uch
  30. End If
  31. End If
  32. Next

  33. toUTF8 = szRet
  34. End Function
复制代码

3、'GB转unicode---将GB编码文字转换为unicode编码文字
  1. function chinese2unicode(Str)
  2. dim i
  3. dim Str_one
  4. dim Str_unicode
  5. if(isnull(Str)) then
  6. exit function
  7. end if
  8. for i=1 to len(Str)
  9. Str_one=Mid(Str,i,1)
  10. Str_unicode=Str_unicode&chr(38)
  11. Str_unicode=Str_unicode&chr(35)
  12. Str_unicode=Str_unicode&chr(120)
  13. Str_unicode=Str_unicode& Hex(ascw(Str_one))
  14. Str_unicode=Str_unicode&chr(59)
  15. next
  16. chinese2unicode=Str_unicode
  17. end function
复制代码

4、'URL解码
  1. Function URLDecode(enStr)
  2. dim deStr
  3. dim c,i,v
  4. deStr=""
  5. for i=1 to len(enStr)
  6. c=Mid(enStr,i,1)
  7. if c="%" then
  8. v=eval("&h"+Mid(enStr,i+1,2))
  9. if v<128 then
  10. deStr=deStr&chr(v)
  11. i=i+2
  12. else
  13. if isvalidhex(mid(enstr,i,3)) then
  14. if isvalidhex(mid(enstr,i+3,3)) then
  15. v=eval("&h"+Mid(enStr,i+1,2)+Mid(enStr,i+4,2))
  16. deStr=deStr&chr(v)
  17. i=i+5
  18. else
  19. v=eval("&h"+Mid(enStr,i+1,2)+cstr(hex(asc(Mid(enStr,i+3,1)))))
  20. deStr=deStr&chr(v)
  21. i=i+3
  22. end if
  23. else
  24. destr=destr&c
  25. end if
  26. end if
  27. else
  28. if c="+" then
  29. deStr=deStr&" "
  30. else
  31. deStr=deStr&c
  32. end if
  33. end if
  34. next
  35. URLDecode=deStr
  36. end function

  37. '判断是否为有效的十六进制代码
  38. function isvalidhex(str)
  39. dim c
  40. isvalidhex=true
  41. str=ucase(str)
  42. if len(str)<>3 then isvalidhex=false:exit function
  43. if left(str,1)<>"%" then isvalidhex=false:exit function
  44. c=mid(str,2,1)
  45. if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit function
  46. c=mid(str,3,1)
  47. if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit function
  48. end function
  49. %>
复制代码



上一篇:VBScript ASP CDbl() 函数转换为双精度类型
下一篇:asp Fix、Int、Round、CInt函数使用说明
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 18:26

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表