百度空间 | 百度首页 
 
查看文章
 
ASP汉字简体繁体内码互相转换GB<==>Big5(修正版)
2008-05-24 23:36

<%
'covert big=>gb gb=>big5
'------- begin ------------------
'ASP汉字简体繁体内码互相转换GB<==>Big5(修正版)
'2008-05-24 by jun 修复BUG,并支持简繁互转
Function gb_big5(inputText)
tablePath="/include/big5.map"
gb_big5=convert_gb_big5(inputText,tablePath)
End Function

Function big5_gb(inputText)
tablePath="/include/gb.map"
big5_gb=convert_gb_big5(inputText,tablePath)
End Function

Function convert_gb_big5(inputText,tablePath)
Dim Ados,Max,i,c,b,h,l
If IsNull(inputText) Or inputText = "" Then Exit Function

Set Ados=Server.CreateObject("Adodb.Stream")
Ados.Mode = 3
Ados.Type = 1
Ados.Open
Ados.LoadFromFile(Server.MapPath(tablePath))

Text = Server.UrlEnCode(inputText)
b = ""
Max = Len(Text)
For i=1 To Max
c = Mid(Text,i,1)
If c = "%" Then
h = eval("&h"+Mid(Text,i+1,2))
If h < 128 Then
b = b & chr(h)
i = i+2
Else
If isvalidhex(mid(Text,i,3)) Then
If isvalidhex(mid(Text,i+3,3)) Then
l = eval("&h"+Mid(Text,i+4,2))
i = i+5
Else
l = eval("&h"+cstr(hex(asc(mid(Text,i+3,1)))))
i = i+3
End If
if (h-160)*510+(l-1)*2>0 then Ados.Position = (h-160)*510+(l-1)*2
b = b & bytes2BSTR(Ados.Read(2))
End If
End If
Else
If c = "+" Then
b = b & " "
Else
b = b & c
End If
End If
Next
Set Ados = Nothing
convert_gb_big5 = b
End Function


Function isvalidhex(str)
isvalidhex=true
str=ucase(str)
If Len(str) <> 3 Then isvalidhex = false : Exit Function
if Left(str,1) <> "%" Then isvalidhex = false : Exit Function
c = Mid(str,2,1)
If Not (((c>="0") And (c<="9")) Or ((c>="A") And (c<="Z"))) Then isvalidhex = false : Exit Function
c = Mid(str,3,1)
If Not (((c>="0") And (c<="9")) Or ((c>="A") And (c<="Z"))) Then isvalidhex = false : Exit Function
End Function

Function bytes2BSTR(vIn)
Dim strReturn
Dim i,ThisCharCode,NextCharCode
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
'------- end ------------------


gbStr="ASP汉字简体繁体内码互相转换GB<==>Big5(修正版)"

big5String=gb_big5(gbStr)

gbString=big5_gb(big5String)

response.write big5String
response.write "<br>"
response.write gbString

%>


类别:Asp | 添加到搜藏 | 浏览() | 评论 (1)
 
最近读者:
 
网友评论:
1
2008-07-07 10:32 | 回复
来个gb.map 和 big.map的下载连接 网上找了半天都没有gb.map
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu