就他吧-9ta8为您提供:身份证查询、15位转16位身份证,手机号码归属地查询,IP地址查询服务,城市天气预报查询,列车时刻表简易快速查询等等查询服务,就他吧欢迎您的光临!!

ASP写的汉字转换UTF-8及UTF-8转GB2312

  1汉字转换为UTF-8
  2Function trans(str)
  3    dim strlen
  4    dim conertstr
  5    if IsNull(strthen
  6        Trans=str
  7    else
  8        position=1
  9        strlen=Len(str)
 10        dim j
 11        for j=1 to strlen
 12            convertstr=convertstr & "&#x" & Hex(AscW(Mid(str,j,1))) & ";"
 13        next
 14        Trans=convertstr
 15    end if
 16end Function
 
 17
 18function chinese2unicode(Str
 19  dim i 
 20  dim Str_one 
 21  dim Str_unicode 
 22  for i=1 to len(Str
 23    Str_one=Mid(Str,i,1
 24    Str_unicode=Str_unicode&chr(38
 25    Str_unicode=Str_unicode&chr(35
 26    Str_unicode=Str_unicode&chr(120
 27    Str_unicode=Str_unicode& Hex(ascw(Str_one)) 
 28    Str_unicode=Str_unicode&chr(59
 29  next 
 30  Response.Write Str_unicode 
 31end function
    
 32
 33UTF-8 To GB2312
 34
 35function UTF2GB(UTFStr)
 36    for Dig=1 to len(UTFStr)
 37        if mid(UTFStr,Dig,1)="%" then
 38            if len(UTFStr) >= Dig+8 then
 39                GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9))
 40                Dig=Dig+8
 41            else
 42                GBStr=GBStr & mid(UTFStr,Dig,1)
 43            end if
 44        else
 45            GBStr=GBStr & mid(UTFStr,Dig,1)
 46        end if
 47    next
 48    UTF2GB=GBStr
 49end function
 
 50
 51
 52function ConvChinese(x) 
 53    A=split(mid(x,2),"%")
 54    i=0
 55    j=0
 56    
 57    for i=0 to ubound(A) 
 58        A(i)=c16to2(A(i))
 59    next
 60        
 61    for i=0 to ubound(A)-1
 62        DigS=instr(A(i),"0")
 63        Unicode=""
 64        for j=1 to DigS-1
 65            if j=1 then 
 66                A(i)=right(A(i),len(A(i))-DigS)
 67                Unicode=Unicode & A(i)
 68            else
 69                i=i+1
 70                A(i)=right(A(i),len(A(i))-2)
 71                Unicode=Unicode & A(i) 
 72            end if 
 73        next
 74        
 75        if len(c2to16(Unicode))=4 then
 76            ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode)))
 77        else
 78            ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode)))
 79        end if
 80    next
 81end function

 82
 83function c2to16(x)
 84    i=1
 85    for i=1 to len(x)  step 4 
 86        c2to16=c2to16 & hex(c2to10(mid(x,i,4))) 
 87    next
 88end function
 
 89    
 90function c2to10(x)
 91    c2to10=0
 92    if x="0" then exit function
 93    i=0
 94    for i= 0 to len(x) -1
 95        if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i)
 96    next 
 97end function

 98
 99function c16to2(x)
100    i=0
101    for i=1 to len(trim(x)) 
102        tempstr= c10to2(cint(int("&h" & mid(x,i,1))))
103        do while len(tempstr)<4
104        tempstr="0" & tempstr
105        loop
106        c16to2=c16to2 & tempstr
107    next
108end function

109
110function c10to2(x)
111    mysign=sgn(x)
112    x=abs(x)
113    DigS=1
114    do 
115        if x<2^DigS then
116            exit do
117        else
118            DigS=DigS+1
119        end if
120    loop
121    tempnum=x
122    
123    i=0
124    for i=DigS to 1 step-1
125        if tempnum>=2^(i-1then
126            tempnum=tempnum-2^(i-1)
127            c10to2=c10to2 & "1"   
128        else
129            c10to2=c10to2 & "0"
130        end if
131    next
132    if mysign=-1 then c10to2="-" & c10to2
133end function

134
posted on 2005-10-20 17:37  振河  阅读(3637)  评论(0)    收藏  举报

  就他吧-9ta8伴您开心每一天