现在,我们开始转入正题,首先,我们了解一下编码转换的过程,一段文字的编码如果要转换,我们就要知道当前文字所对应的编码集,然后用对应的编码集去读取,这是编码正确被转换的前提(如果这步弄错了,那么转换出来的就会是一连串乱码)!好了,找对了当前编码,并且正确读取出来了以后,我们就可以用设定好目标编码的Stream对象去保存这个内容到指定文件中了,到这里我们的编码转换就成功了! 下面我们来看一下asp编码转换的具体代码实现: 复制代码 代码如下: "转换编码 content 要转换编码的内容, cset 目标编码, dest 目标文件绝对路径 Function TransferCharSet(content, cset, dest) Dim Objstream Set Objstream = Server.CreateObject("adodb.stream") objstream.Mode =3 objstream.Charset = cset objstream.Type = 2 objstream.Open objstream.WriteText content objstream.Position = 0 objstream.SaveToFile dest,2 objstream.Close set objstream = nothing End Function "用对应编码读取指定内容 Function getcontent(path) Dim Objstream Set Objstream = Server.CreateObject("Adodb.Stream") objstream.Charset = GetCharSetName(path) objstream.Type = 2 objstream.Mode =3 "objstream.Charset = code objstream.Open Objstream.LoadFromFile path objstream.Position = 0 getcontent = objstream.ReadText objstream.Close set objstream = nothing End Function "取得指定内容的编码名称 Function GetCharSetName(path) Set objstream=server.createobject("Adodb.Stream") objstream.Type=1 objstream.mode=3 objstream.open objstream.Position=0 objstream.loadfromfile path bintou=objstream.read(2) If AscB(MidB(bintou,1,1))=&HEF And AscB(MidB(bintou,2,1))=&HBB Then GetCharSetName="utf-8" ElseIf AscB(MidB(bintou,1,1))=&HFF And AscB(MidB(bintou,2,1))=&HFE Then GetCharSetName="unicode" Else GetCharSetName="gb2312" End If objstream.close Set objstream=nothing End Function