zoukankan      html  css  js  c++  java
  • asp中有关字符编码转换的几个函数

      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 字符代码,注意区别
    125        nAsc = AscW(wch)
    126        If nAsc < 0 Then nAsc = nAsc + 65536
    127    
    128        If (nAsc And &HFF80) = 0 Then
    129            szRet = szRet & wch
    130        Else
    131            If (nAsc And &HF000) = 0 Then
    132                uch = "%" & Hex(((nAsc \ 2 ^ 6)) Or &HC0) & Hex(nAsc And &H3F Or &H80)
    133                szRet = szRet & uch
    134            Else
    135               'GB编码文字的Unicode字符代码在0800 - FFFF之间采用三字节模版
    136                uch = "%" & Hex((nAsc \ 2 ^ 12Or &HE0) & "%" & _
    137                            Hex((nAsc \ 2 ^ 6And &H3F Or &H80) & "%" & _
    138                            Hex(nAsc And &H3F Or &H80)
    139                szRet = szRet & uch
    140            End If
    141        End If
    142    Next
    143        
    144    toUTF8 = szRet
    145End Function
    146
    1473'GB转unicode---将GB编码文字转换为unicode编码文字
    148
    149function chinese2unicode(Str) 
    150  dim i 
    151  dim Str_one 
    152  dim Str_unicode 
    153  if(isnull(Str)) then
    154     exit function
    155  end if
    156  for i=1 to len(Str) 
    157    Str_one=Mid(Str,i,1
    158    Str_unicode=Str_unicode&chr(38
    159    Str_unicode=Str_unicode&chr(35
    160    Str_unicode=Str_unicode&chr(120
    161    Str_unicode=Str_unicode& Hex(ascw(Str_one)) 
    162    Str_unicode=Str_unicode&chr(59
    163  next 
    164  chinese2unicode=Str_unicode 
    165end function   
    166
    1674'URL解码
    168Function URLDecode(enStr)
    169dim deStr
    170dim c,i,v
    171deStr=""
    172for i=1 to len(enStr)
    173  c=Mid(enStr,i,1)
    174  if c="%" then
    175   v=eval("&h"+Mid(enStr,i+1,2))
    176   if v<128 then
    177    deStr=deStr&chr(v)
    178    i=i+2
    179   else
    180    if isvalidhex(mid(enstr,i,3)) then
    181     if isvalidhex(mid(enstr,i+3,3)) then
    182      v=eval("&h"+Mid(enStr,i+1,2)+Mid(enStr,i+4,2))
    183      deStr=deStr&chr(v)
    184      i=i+5
    185     else
    186      v=eval("&h"+Mid(enStr,i+1,2)+cstr(hex(asc(Mid(enStr,i+3,1)))))
    187      deStr=deStr&chr(v)
    188      i=i+3 
    189     end if 
    190    else 
    191     destr=destr&c
    192    end if
    193   end if
    194  else
    195   if c="+" then
    196    deStr=deStr&" "
    197   else
    198    deStr=deStr&c
    199   end if
    200  end if
    201next
    202URLDecode=deStr
    203end function
    204
    205'判断是否为有效的十六进制代码
    206function isvalidhex(str)
    207dim c
    208isvalidhex=true
    209str=ucase(str)
    210if len(str)<>3 then isvalidhex=false:exit function
    211if left(str,1)<>"%" then isvalidhex=false:exit function
    212  c=mid(str,2,1)
    213if not (((c>="0"and (c<="9")) or ((c>="A"and (c<="Z"))) then isvalidhex=false:exit function
    214  c=mid(str,3,1)
    215if not (((c>="0"and (c<="9")) or ((c>="A"and (c<="Z"))) then isvalidhex=false:exit function
    216end function
    217
    %>
    218
  • 相关阅读:
    Java基础知识(一)环境变量的设置、变量与常量、自动类型转换、自增长、赋值运算符
    ModelState
    DOM
    正则表达式常见形式
    通过JS设置一个元素的文本
    JS(三) 原型对象与原型链
    JS(二)call方法和apply方法
    第四周学习进度表
    敏捷开发方法综述
    数组02
  • 原文地址:https://www.cnblogs.com/meil/p/638095.html
Copyright © 2011-2022 走看看