现在的位置: 首页 > 综合 > 正文

asp中有关字符编码转换的几个函数

2012年08月05日 ⁄ 综合 ⁄ 共 2773字 ⁄ 字号 评论关闭
  1<%
  21'UTF转GB---将UTF8编码文字转换为GB编码文字
  3function UTF2GB(UTFStr) 
  4
  5for Dig=1 to len(UTFStr) 
  6  '如果UTF8编码文字以%开头则进行转换
  7  if mid(UTFStr,Dig,1)="%" then 
  8     'UTF8编码文字大于8则转换为汉字
  9    if len(UTFStr) >= Dig+8 then 
 10       GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9)) 
 11       Dig=Dig+8 
 12    else 
 13      GBStr=GBStr & mid(UTFStr,Dig,1
 14    end if 
 15  else 
 16     GBStr=GBStr & mid(UTFStr,Dig,1
 17  end if 
 18next 
 19UTF2GB=GBStr 
 20end function 
 21
 22'UTF8编码文字将转换为汉字
 23function ConvChinese(x) 
 24   A=split(mid(x,2),"%"
 25   i=0 
 26   j=0 
 27  for i=0 to ubound(A) 
 28     A(i)=c16to2(A(i)) 
 29  next 
 30  for i=0 to ubound(A)-1 
 31    DigS=instr(A(i),"0"
 32    Unicode="" 
 33    for j=1 to DigS-1 
 34      if j=1 then 
 35        A(i)=right(A(i),len(A(i))-DigS) 
 36        Unicode=Unicode & A(i) 
 37      else 
 38         i=i+1 
 39         A(i)=right(A(i),len(A(i))-2
 40         Unicode=Unicode & A(i) 
 41      end if 
 42    next 
 43
 44    if len(c2to16(Unicode))=4 then 
 45       ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode))) 
 46    else 
 47       ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode))) 
 48    end if 
 49  next 
 50end function 
 51
 52'二进制代码转换为十六进制代码
 53function c2to16(x)
 54   i=1 
 55   for i=1 to len(x) step 4 
 56      c2to16=c2to16 & hex(c2to10(mid(x,i,4))) 
 57   next 
 58end function 
 59
 60'二进制代码转换为十进制代码
 61function c2to10(x)
 62   c2to10=0 
 63   if x="0" then exit function 
 64     i=0 
 65   for i= 0 to len(x) -1 
 66      if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i) 
 67   next 
 68end function 
 69
 70'十六进制代码转换为二进制代码
 71function c16to2(x) 
 72    i=0 
 73    for i=1 to len(trim(x)) 
 74      tempstr= c10to2(cint(int("&h" & mid(x,i,1)))) 
 75      do while len(tempstr)<4 
 76         tempstr="0" & tempstr 
 77      loop 
 78      c16to2=c16to2 & tempstr 
 79   next 
 80end function 
 81
 82'十进制代码转换为二进制代码
 83function c10to2(x) 
 84   mysign=sgn(x) 
 85   x=abs(x) 
 86   DigS=1 
 87   do 
 88      if x<2^DigS then 
 89        exit do 
 90      else 
 91        DigS=DigS+1 
 92      end if 
 93   loop 
 94   tempnum=
 95
 96   i=0 
 97   for i=DigS to 1 step-1 
 98      if tempnum>=2^(i-1then 
 99         tempnum=tempnum-2^(i-1
100         c10to2=c10to2 & "1" 
101      else 
102         c10to2=c10to2 & "0" 
103      end if 
104   next 
105   if mysign=-1 then c10to2="-" & c10to2 
106end function
107
1082'GB转UTF8--将GB编码文字转换为UTF8编码文字
109
110Function toUTF8(szInput)
111    Dim wch, uch, szRet
112    Dim x
113    Dim nAsc, nAsc2, nAsc3
114    '如果输入参数为空,则退出函数
115    If szInput = "" Then
116        toUTF8 = szInput
117        Exit Function
118    End If
119    '开始转换
120     For x = 1 To Len(szInput)
121        '利用mid函数分拆GB编码文字
122        wch = Mid(szInput, x, 1)
123        '利用ascW函数返回每一个GB编码文字的Unicode字符代码
124        '注:asc函数返回的是ANSI 字符代码,注意区

抱歉!评论已关闭.