Recently, I am working on this thing and I can't find any Asp-related things online. If someone makes it very early, don't joke. It took a long time to finally get it done;
Principle: Use the stream object to read the first two bytes of the file, analyze and determine the encoding of utf-8, unicode, and ANSI (simplified Chinese operating system, i.e. gb2312)
Related information:
ANSI: Unformatted definition;
Unicode: The first two bytes are FFFE;
Unicodebigendian: The first two bytes are FEFF;
UTF-8: The first two bytes are EFBB;
Copy the code as follows: functioncheckcode(path)
setobjstream=server.createobject("adodb.stream")
objstream.Type=1
objstream.mode=3
objstream.open
objstream.Position=0
objstream.loadfromfilepath
bintou=objstream.read(2)
IfAscB(MidB(bintou,1,1))=&HEFAndAscB(MidB(bintou,2,1))=&HBBThen
checkcoder="utf-8"
ElseIfAscB(MidB(bintou,1,1))=&HFFAndAscB(MidB(bintou,2,1))=&HFEThen
checkcode="unicode"
Else
checkcode="gb2312"
EndIf
objstream.close
setobjstream=nothing
endfunction
Replenish:
Xie Xiaoyu reminded that the previous one was simply nonsense; the local encoding of ANSI is defined by each country itself, and there is no fixed file header format. Under the mainland Chinese operating system, it is readable gb2312, and under other language systems, it is garbled, so there is no need to distinguish this part in detail.
Get the file encoding, and the stream stream can be opened as required encoding, and there will be no garbled code.