ASP UTF-8 page garbled + gb2312 to UTF-8 + generate UTF-8 format file (encoding) page 1/2

Source: Internet
Author: User

The best method:
Let's talk about the basic things:
<% @ CodePage = 65001%> UTF-8
<% @ CodePage = 936%> Simplified Chinese
<% @ CodePage = 950%> traditional Chinese
<% @ CodePage = 437%> American/Canadian English
<% @ CodePage = 932%> Japanese
<% @ CodePage = 949%> Korean
<% @ CodePage = 866%> Russian

CodePage specifies the encoding used by IIS to read passed strings (such as form submission and address bar transfer ).

The cause of garbled characters is that the module encoding is different when the website is to be integrated.
Like my blog, this problem occurs during integration, because the blog is Utf-8,
Recently, many netizens have been asking for this question. I have tried many methods.
The most convenient method is as follows:
Do not convert the encoding of the webpage in any module, such as UTF-8 or UTF-8, or gb22312 or gb2312.
Add
<% @ Language = "VBScript" codePage = "65001" %>
<% Session. codePage = 65001%>
Add
<% @ Language = "VBScript" codePage = "936" %>
<% Session. codePage = 936%>
Other codes.
Mutual conversion between Chinese characters and UTF-8 in ASP
'================== Convert Chinese characters to UTF-8 ==============================

Function chinese2unicode (STR)
For I = 1 to Len (STR)
Str_one = mid (STR, I, 1)
Str_unicode = str_unicode & CHR (38)
Str_unicode = str_unicode & CHR (35)
Str_unicode = str_unicode & CHR (1, 120)
Str_unicode = str_unicode & hex (ASCW (str_one ))
Str_unicode = str_unicode & CHR (59)
Next
Chinese2unicode = str_unicode
End Function

'================ UTF-8 to convert Chinese characters ========================================

Function utf2gb (utfstr)
For dig = 1 to Len (utfstr)
If mid (utfstr, dig, 1) = "%" then
If Len (utfstr)> = dig + 8 then
Gbstr = gbstr & convchinese (mid (utfstr, dig, 9 ))
Dig = dig + 8
Else
Gbstr = gbstr & Mid (utfstr, dig, 1)
End if
Else
Gbstr = gbstr & Mid (utfstr, dig, 1)
End if
Next
Utf2gb = gbstr
End Function

Function convchinese (X)
A = Split (mid (x, 2), "% ")
I = 0
J = 0

For I = 0 to ubound ()
A (I) = c16to2 (A (I ))
Next

For I = 0 to ubound (a)-1
Digs = instr (a (I), "0 ")
Unicode = ""
For j = 1 to digs-1
If j = 1 then
A (I) = right (a (I), Len (A (I)-Digs)
Unicode = Unicode & A (I)
Else
I = I + 1
A (I) = right (a (I), Len (A (I)-2)
Unicode = Unicode & A (I)
End if
Next

If Len (c2to16 (UNICODE) = 4 then
Convchinese = convchinese & chrw (INT ("& H" & c2to16 (UNICODE )))
Else
Convchinese = convchinese & CHR (INT ("& H" & c2to16 (UNICODE )))
End if
Next
End Function

Function c2to16 (X)
I = 1
For I = 1 to Len (x) Step 4
C2to16 = c2to16 & hex (c2to10 (mid (X, I, 4 )))
Next
End Function

Function c2to10 (X)
C2to10 = 0
If X = "0" then exit function
I = 0
For I = 0 to Len (x)-1
If mid (x, Len (x)-I, 1) = "1" then c2to10 = c2to10 + 2 ^ (I)
Next
End Function

Function c16to2 (X)
I = 0
For I = 1 to Len (TRIM (x ))
Tempstr = c10to2 (CINT ("& H" & Mid (X, I, 1 ))))
Do While Len (tempstr) <4
Tempstr = "0" & tempstr
Loop
C16to2 = c16to2 & tempstr
Next
End Function

Function c10to2 (X)
Mysign = SGN (X)
X = ABS (X)
Digs = 1
Do
If x <2 ^ digs then
Exit do
Else
Digs = digs + 1
End if
Loop
Tempnum = x

I = 0
For I = digs to 1 step-1
If tempnum> = 2 ^ (I-1) then
Tempnum = tempnum-2 ^ (I-1)
C10to2 = c10to2 & "1"
Else
C10to2 = c10to2 & "0"
End if
Next
If mysign =-1 then c10to2 = "-" & c10to2
End Function

Gb2312 to UTF-8

'IndividualsCodeStyle annotation (the first lowercase alphabet in the variable name represents the variable type)
'I: Integer type;
'S: string;
Function u2utf8 (byval a_inum)
Dim sresult, suf8
Dim itemp, ihexnum, I

Ihexnum = trim (a_inum)

If ihexnum = "" then
Exit Function
End if

Sresult = ""

If (ihexnum <128) then
Sresult = sresult & ihexnum
Elseif (ihexnum <2048) then
Sresult = chrb (& h80 + (ihexnum and & h3f ))
Ihexnum = ihexnum \ & h40
Sresult = chrb (& hc0 + (ihexnum and & h1f) & sresult
Elseif (ihexnum <65536) then
Sresult = chrb (& h80 + (ihexnum and & h3f ))
Ihexnum = ihexnum \ & h40
Sresult = chrb (& h80 + (ihexnum and & h3f) & sresult
Ihexnum = ihexnum \ & h40
Sresult = chrb (& he0 + (ihexnum and & HF) & sresult
End if

U2utf8 = sresult
End Function

Function gb2utf (byval a_sstr)
Dim SGB, sresult, stemp
Dim ilen, iunicode, itemp, I

SGB = trim (a_sstr)
Ilen = Len (SGB)
For I = 1 to ilen
Stemp = mid (SGB, I, 1)
Itemp = ASC (stemp)

If (itemp> 127 or itemp <0) then
Iunicode = ASCW (stemp)
If iunicode <0 then
Iunicode = iunicode + 65536
End if
Else
Iunicode = itemp
End if

Sresult = sresult & u2utf8 (iunicode)
Next

Gb2utf = sresult
End Function

'Call Method
Response. binarywrite (gb2utf ("Chinese "))

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.