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

  1 ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif
  2InBlock.gif1'UTF转GB---将UTF8编码文字转换为GB编码文字
  3InBlock.giffunction UTF2GB(UTFStr) 
  4InBlock.gif
  5InBlock.giffor Dig=1 to len(UTFStr) 
  6InBlock.gif  '如果UTF8编码文字以%开头则进行转换
  7InBlock.gif  if mid(UTFStr,Dig,1)="%" then 
  8InBlock.gif     'UTF8编码文字大于8则转换为汉字
  9InBlock.gif    if len(UTFStr) >= Dig+8 then 
 10InBlock.gif       GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9)) 
 11InBlock.gif       Dig=Dig+8 
 12InBlock.gif    else 
 13InBlock.gif      GBStr=GBStr & mid(UTFStr,Dig,1
 14InBlock.gif    end if 
 15InBlock.gif  else 
 16InBlock.gif     GBStr=GBStr & mid(UTFStr,Dig,1
 17InBlock.gif  end if 
 18InBlock.gifnext 
 19InBlock.gifUTF2GB=GBStr 
 20InBlock.gifend function 
 21InBlock.gif
 22InBlock.gif'UTF8编码文字将转换为汉字
 23InBlock.giffunction ConvChinese(x) 
 24InBlock.gif   A=split(mid(x,2),"%"
 25InBlock.gif   i=0 
 26InBlock.gif   j=0 
 27InBlock.gif  for i=0 to ubound(A) 
 28InBlock.gif     A(i)=c16to2(A(i)) 
 29InBlock.gif  next 
 30InBlock.gif  for i=0 to ubound(A)-1 
 31InBlock.gif    DigS=instr(A(i),"0"
 32InBlock.gif    Unicode="" 
 33InBlock.gif    for j=1 to DigS-1 
 34InBlock.gif      if j=1 then 
 35InBlock.gif        A(i)=right(A(i),len(A(i))-DigS) 
 36InBlock.gif        Unicode=Unicode & A(i) 
 37InBlock.gif      else 
 38InBlock.gif         i=i+1 
 39InBlock.gif         A(i)=right(A(i),len(A(i))-2
 40InBlock.gif         Unicode=Unicode & A(i) 
 41InBlock.gif      end if 
 42InBlock.gif    next 
 43InBlock.gif
 44InBlock.gif    if len(c2to16(Unicode))=4 then 
 45InBlock.gif       ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode))) 
 46InBlock.gif    else 
 47InBlock.gif       ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode))) 
 48InBlock.gif    end if 
 49InBlock.gif  next 
 50InBlock.gifend function 
 51InBlock.gif
 52InBlock.gif'二进制代码转换为十六进制代码
 53InBlock.giffunction c2to16(x)
 54InBlock.gif   i=1 
 55InBlock.gif   for i=1 to len(x) step 4 
 56InBlock.gif      c2to16=c2to16 & hex(c2to10(mid(x,i,4))) 
 57InBlock.gif   next 
 58InBlock.gifend function 
 59InBlock.gif
 60InBlock.gif'二进制代码转换为十进制代码
 61InBlock.giffunction c2to10(x)
 62InBlock.gif   c2to10=0 
 63InBlock.gif   if x="0" then exit function 
 64InBlock.gif     i=0 
 65InBlock.gif   for i= 0 to len(x) -1 
 66InBlock.gif      if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i) 
 67InBlock.gif   next 
 68InBlock.gifend function 
 69InBlock.gif
 70InBlock.gif'十六进制代码转换为二进制代码
 71InBlock.giffunction c16to2(x) 
 72InBlock.gif    i=0 
 73InBlock.gif    for i=1 to len(trim(x)) 
 74InBlock.gif      tempstr= c10to2(cint(int("&h" & mid(x,i,1)))) 
 75InBlock.gif      do while len(tempstr)<4 
 76InBlock.gif         tempstr="0" & tempstr 
 77InBlock.gif      loop 
 78InBlock.gif      c16to2=c16to2 & tempstr 
 79InBlock.gif   next 
 80InBlock.gifend function 
 81InBlock.gif
 82InBlock.gif'十进制代码转换为二进制代码
 83InBlock.giffunction c10to2(x) 
 84InBlock.gif   mysign=sgn(x) 
 85InBlock.gif   x=abs(x) 
 86InBlock.gif   DigS=1 
 87InBlock.gif   do 
 88InBlock.gif      if x<2^DigS then 
 89InBlock.gif        exit do 
 90InBlock.gif      else 
 91InBlock.gif        DigS=DigS+1 
 92InBlock.gif      end if 
 93InBlock.gif   loop 
 94InBlock.gif   tempnum=
 95InBlock.gif
 96InBlock.gif   i=0 
 97InBlock.gif   for i=DigS to 1 step-1 
 98InBlock.gif      if tempnum>=2^(i-1then 
 99InBlock.gif         tempnum=tempnum-2^(i-1
100InBlock.gif         c10to2=c10to2 & "1" 
101InBlock.gif      else 
102InBlock.gif         c10to2=c10to2 & "0" 
103InBlock.gif      end if 
104InBlock.gif   next 
105InBlock.gif   if mysign=-1 then c10to2="-" & c10to2 
106InBlock.gifend function
107InBlock.gif
108InBlock.gif2'GB转UTF8--将GB编码文字转换为UTF8编码文字
109InBlock.gif
110InBlock.gifFunction toUTF8(szInput)
111InBlock.gif    Dim wch, uch, szRet
112InBlock.gif    Dim x
113InBlock.gif    Dim nAsc, nAsc2, nAsc3
114InBlock.gif    '如果输入参数为空,则退出函数
115InBlock.gif    If szInput = "" Then
116InBlock.gif        toUTF8 = szInput
117InBlock.gif        Exit Function
118InBlock.gif    End If
119InBlock.gif    '开始转换
120InBlock.gif     For x = 1 To Len(szInput)
121InBlock.gif        '利用mid函数分拆GB编码文字
122InBlock.gif        wch = Mid(szInput, x, 1)
123InBlock.gif        '利用ascW函数返回每一个GB编码文字的Unicode字符代码
124InBlock.gif        '注:asc函数返回的是ANSI 字符代码,注意区别
125InBlock.gif        nAsc = AscW(wch)
126InBlock.gif        If nAsc < 0 Then nAsc = nAsc + 65536
127InBlock.gif    
128InBlock.gif        If (nAsc And &HFF80) = 0 Then
129InBlock.gif            szRet = szRet & wch
130InBlock.gif        Else
131InBlock.gif            If (nAsc And &HF000) = 0 Then
132InBlock.gif                uch = "%" & Hex(((nAsc \ 2 ^ 6)) Or &HC0) & Hex(nAsc And &H3F Or &H80)
133InBlock.gif                szRet = szRet & uch
134InBlock.gif            Else
135InBlock.gif               'GB编码文字的Unicode字符代码在0800 - FFFF之间采用三字节模版
136InBlock.gif                uch = "%" & Hex((nAsc \ 2 ^ 12Or &HE0) & "%" & _
137InBlock.gif                            Hex((nAsc \ 2 ^ 6And &H3F Or &H80) & "%" & _
138InBlock.gif                            Hex(nAsc And &H3F Or &H80)
139InBlock.gif                szRet = szRet & uch
140InBlock.gif            End If
141InBlock.gif        End If
142InBlock.gif    Next
143InBlock.gif        
144InBlock.gif    toUTF8 = szRet
145InBlock.gifEnd Function
146InBlock.gif
147InBlock.gif3'GB转unicode---将GB编码文字转换为unicode编码文字
148InBlock.gif
149InBlock.giffunction chinese2unicode(Str) 
150InBlock.gif  dim i 
151InBlock.gif  dim Str_one 
152InBlock.gif  dim Str_unicode 
153InBlock.gif  if(isnull(Str)) then
154InBlock.gif     exit function
155InBlock.gif  end if
156InBlock.gif  for i=1 to len(Str) 
157InBlock.gif    Str_one=Mid(Str,i,1
158InBlock.gif    Str_unicode=Str_unicode&chr(38
159InBlock.gif    Str_unicode=Str_unicode&chr(35
160InBlock.gif    Str_unicode=Str_unicode&chr(120
161InBlock.gif    Str_unicode=Str_unicode& Hex(ascw(Str_one)) 
162InBlock.gif    Str_unicode=Str_unicode&chr(59
163InBlock.gif  next 
164InBlock.gif  chinese2unicode=Str_unicode 
165InBlock.gifend function   
166InBlock.gif
167InBlock.gif4'URL解码
168InBlock.gifFunction URLDecode(enStr)
169InBlock.gifdim deStr
170InBlock.gifdim c,i,v
171InBlock.gifdeStr=""
172InBlock.giffor i=1 to len(enStr)
173InBlock.gif  c=Mid(enStr,i,1)
174InBlock.gif  if c="%" then
175InBlock.gif   v=eval("&h"+Mid(enStr,i+1,2))
176InBlock.gif   if v<128 then
177InBlock.gif    deStr=deStr&chr(v)
178InBlock.gif    i=i+2
179InBlock.gif   else
180InBlock.gif    if isvalidhex(mid(enstr,i,3)) then
181InBlock.gif     if isvalidhex(mid(enstr,i+3,3)) then
182InBlock.gif      v=eval("&h"+Mid(enStr,i+1,2)+Mid(enStr,i+4,2))
183InBlock.gif      deStr=deStr&chr(v)
184InBlock.gif      i=i+5
185InBlock.gif     else
186InBlock.gif      v=eval("&h"+Mid(enStr,i+1,2)+cstr(hex(asc(Mid(enStr,i+3,1)))))
187InBlock.gif      deStr=deStr&chr(v)
188InBlock.gif      i=i+3 
189InBlock.gif     end if 
190InBlock.gif    else 
191InBlock.gif     destr=destr&c
192InBlock.gif    end if
193InBlock.gif   end if
194InBlock.gif  else
195InBlock.gif   if c="+" then
196InBlock.gif    deStr=deStr&" "
197InBlock.gif   else
198InBlock.gif    deStr=deStr&c
199InBlock.gif   end if
200InBlock.gif  end if
201InBlock.gifnext
202InBlock.gifURLDecode=deStr
203InBlock.gifend function
204InBlock.gif
205InBlock.gif'判断是否为有效的十六进制代码
206InBlock.giffunction isvalidhex(str)
207InBlock.gifdim c
208InBlock.gifisvalidhex=true
209InBlock.gifstr=ucase(str)
210InBlock.gifif len(str)<>3 then isvalidhex=false:exit function
211InBlock.gifif left(str,1)<>"%" then isvalidhex=false:exit function
212InBlock.gif  c=mid(str,2,1)
213InBlock.gifif not (((c>="0"and (c<="9")) or ((c>="A"and (c<="Z"))) then isvalidhex=false:exit function
214InBlock.gif  c=mid(str,3,1)
215InBlock.gifif not (((c>="0"and (c<="9")) or ((c>="A"and (c<="Z"))) then isvalidhex=false:exit function
216ExpandedBlockEnd.gifend function
217None.gif
%>
218 None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值