2013年9月25日 星期三

[ASP] UTF-8跟BIG5互轉

BIG5 --> UTF-8
Function Convert_ASCII_Unicode(str)
    Dim old,new_w,j
    old = str
    new_w = ""
    For j = 1 To Len(str)
        if ascw(mid(old,j,1)) < 0 then
            new_w = new_w & "&#" & ascw(mid(old,j,1)) + 65536 & ";"
        ElseIf ascw(mid(old,j,1))>0 and ascw(mid(old,j,1))<127 then
            new_w = new_w & mid(old,j,1)
        Else
            new_w = new_w & "&#" & ascw(mid(old,j,1)) & ";"
        End if
    Next
    Convert_ASCII_Unicode = new_w
End Function

UTF-8 --> BIG5
Function Convert_Unicode_ASCII(str)
    Dim x,y,z,temp_word,flag
    flag = 0
    x = InStr(flag + 1,str,"&#")
    Do Until x = 0 or x < flag
        x = InStr(flag + 1,str,"&#")
        if x <> 0 then
            y = Mid(str,x,8)
            Select Case InStr(y,";")
                Case 8
                    z = chrw(Mid(y,3,5))
                Case 7
                    z = chrw(Mid(y,3,4))
                Case 6
                    z = chrw(Mid(y,3,3))
                Case 5
                    z = chrw(Mid(y,3,2))
            End Select
            if InStr(y,";") > 4 And Asc(z) <> 63 then
                str = Replace(str,Left(y,InStr(y,";")),z)
            End if
             flag = x
         End if
    Loop
    Convert_Unicode_ASCII = str
End Function