On April 11, 2009, I had already completed the code implementation, but I never had time to write this article, so I took the time today to make up for this article, and also for our ASP enthusiasts Hand over a delicious soup to fill the gap in asp encoding conversion. Article background:
One day, a colleague of mine showed me an article on CSDN about programming language rankings. I saw in it that VB ranked very well, so I said, asp (vbscript) is indeed pretty good. As a result, he refuted me and said that ASP is not good and many things are difficult to achieve. What did I say? I have developed ASP for several years and have not found anything difficult to implement, unless it is within the scope of non-scripting languages, such as multi-threaded applications (of course, ASP can also simulate multi-threaded tasks), etc. As a result, he talked about multi-threading, code conversion, etc. He said that he had spent a lot of effort to convert the code without success, so he felt that ASP was too weak. Hearing this, we knew that his programming foundation was not solid enough, and his knowledge of ASP was very limited (and later he said that he did not know or use several commonly used basic objects of ASP. I almost fainted. In the past - I haven't even used this before and told me that ASP is weak. , it seems... I'm so old, I don't even understand that there is no basis and no right to speak! Haha...), so, in a fit of anger, I realized the problem of ASP encoding mutual conversion that weekend. Let him see what ASP is. He actually looks down on ASP so much. He gets very angry just thinking about it! Of course, my research also fills the gap in the research on encoding conversion in ASP development so far!
Now, let's get down to business. First, let's understand the process of encoding conversion. If we want to convert the encoding of a piece of text, we need to know the encoding set corresponding to the current text, and then use the corresponding encoding set to read it. This is The premise is that the encoding is converted correctly (if this step is wrong, the converted code will be a series of garbled codes)! Okay, after finding the current encoding and reading it correctly, we can use the Stream object with the target encoding set to save the content to the specified file. At this point, our encoding conversion is successful!
Let's take a look at the specific code implementation of asp encoding conversion:
Copy the code code as follows:
'Convert encoding content The content to be converted, cset target encoding, dest target file absolute path
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
'Read the specified content using the corresponding encoding
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
'Get the encoding name of the specified content
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
Okay, after this piece of code, our conversion work is complete! I hope this article can bring some help and inspiration to ASP enthusiasts and other language enthusiasts.
Finally, what I want to say is that learning and using programming languages is not difficult. The important thing is to use your brain more and think about how to implement the functions you need, instead of blaming others and saying that the language is too weak.
Well, I wish you all a happy programming. I will write about the encoding conversion of asp here. I will publish more exciting personal original articles in the future. Thank you all!