Recently, in doing this thing, the Internet can not find an ASP-related, if someone got out early, do not laugh at me; it took a long time to get it done;
Principle: Using the Stream object to read the first two bytes of the file, analyze and determine utf-8,unicode,ansi (Simplified Chinese operating system, that is, gb2312) encoding
Related information:
ANSI: no format definition;
Unicode: The first two bytes are fffe;
Unicode Big endian: The first two bytes are feff;
UTF-8: The first two bytes are efbb;
function checkcode(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
checkcoder="utf-8"
ElseIf AscB(MidB(bintou,1,1))=&HFF And AscB(MidB(bintou,2,1))=&HFE Then
checkcode="unicode"
Else
checkcode="gb2312"
End If
objstream.close
set objstream=nothing
end function
Add:
Reminders, the previous is simply nonsense; ANSI local code, are defined by the countries themselves, there is no fixed file header format, in the mainland Chinese operating system, is readable gb2312, in other languages of the system, is garbled, so this part of the need to further distinguish between the file code, Stream stream can be opened according to the required code, it will not garbled