ASP写的汉字转换UTF-8及UTF-8转GB2312
  1
汉字转换为UTF-8
2
Function trans(str)
3
    dim strlen
4
    dim conertstr
5
    if IsNull(str) then
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
16
end Function 
17
18
function 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 
31
end function    
32
33
UTF-8 To GB2312
34
35
function 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
49
end function 
50
51
52
function 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
81
end function
82
83
function 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
88
end function 
89
    
90
function 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 
97
end function
98
99
function 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
108
end function
109
110
function 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-1) then
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
133
end function
134
汉字转换为UTF-82
Function trans(str)3
    dim strlen4
    dim conertstr5
    if IsNull(str) then6
        Trans=str7
    else8
        position=19
        strlen=Len(str)10
        dim j11
        for j=1 to strlen12
            convertstr=convertstr & "&#x" & Hex(AscW(Mid(str,j,1))) & ";"13
        next14
        Trans=convertstr15
    end if16
end Function 17

18
function 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 31
end function    32

33
UTF-8 To GB231234

35
function UTF2GB(UTFStr)36
    for Dig=1 to len(UTFStr)37
        if mid(UTFStr,Dig,1)="%" then38
            if len(UTFStr) >= Dig+8 then39
                GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9))40
                Dig=Dig+841
            else42
                GBStr=GBStr & mid(UTFStr,Dig,1)43
            end if44
        else45
            GBStr=GBStr & mid(UTFStr,Dig,1)46
        end if47
    next48
    UTF2GB=GBStr49
end function 50

51

52
function ConvChinese(x) 53
    A=split(mid(x,2),"%")54
    i=055
    j=056
    57
    for i=0 to ubound(A) 58
        A(i)=c16to2(A(i))59
    next60
        61
    for i=0 to ubound(A)-162
        DigS=instr(A(i),"0")63
        Unicode=""64
        for j=1 to DigS-165
            if j=1 then 66
                A(i)=right(A(i),len(A(i))-DigS)67
                Unicode=Unicode & A(i)68
            else69
                i=i+170
                A(i)=right(A(i),len(A(i))-2)71
                Unicode=Unicode & A(i) 72
            end if 73
        next74
        75
        if len(c2to16(Unicode))=4 then76
            ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode)))77
        else78
            ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode)))79
        end if80
    next81
end function82

83
function c2to16(x)84
    i=185
    for i=1 to len(x)  step 4 86
        c2to16=c2to16 & hex(c2to10(mid(x,i,4))) 87
    next88
end function 89
    90
function c2to10(x)91
    c2to10=092
    if x="0" then exit function93
    i=094
    for i= 0 to len(x) -195
        if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i)96
    next 97
end function98

99
function c16to2(x)100
    i=0101
    for i=1 to len(trim(x)) 102
        tempstr= c10to2(cint(int("&h" & mid(x,i,1))))103
        do while len(tempstr)<4104
        tempstr="0" & tempstr105
        loop106
        c16to2=c16to2 & tempstr107
    next108
end function109

110
function c10to2(x)111
    mysign=sgn(x)112
    x=abs(x)113
    DigS=1114
    do 115
        if x<2^DigS then116
            exit do117
        else118
            DigS=DigS+1119
        end if120
    loop121
    tempnum=x122
    123
    i=0124
    for i=DigS to 1 step-1125
        if tempnum>=2^(i-1) then126
            tempnum=tempnum-2^(i-1)127
            c10to2=c10to2 & "1"   128
        else129
            c10to2=c10to2 & "0"130
        end if131
    next132
    if mysign=-1 then c10to2="-" & c10to2133
end function134

                    
                

                
            
        
浙公网安备 33010602011771号