Implementation method of ASP detection file coding

Source: Internet
Author: User
Tags format definition

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.