Summary of several useful functions of ASP coding (utf8)

Source: Internet
Author: User

1. Convert 'utf to GB-convert UTF8 encoded text into GB encoded text Copy codeThe Code is as follows: function UTF2GB (UTFStr)

For Dig = 1 to len (UTFStr)
'If the UTF8 encoded text starts with %, the conversion is performed.
If mid (UTFStr, Dig, 1) = "%" then
'Utf8 encoded text greater than 8 is converted to Chinese Characters
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

'Utf8 encoded text will be converted to Chinese Characters
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

'Convert binary code to hexadecimal code
Function c2to16 (x)
I = 1
For I = 1 to len (x) step 4
C2to16 = c2to16 & hex (c2to10 (mid (x, I, 4 )))
Next
End function

'Convert binary code to decimal code
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

'Hexadecimal code converted to binary code
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

'Convert decimal code to binary code
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

2. Convert 'gb to UTF8-convert the GB-encoded text into UTF8-encoded textCopy codeThe Code is as follows: Function toUTF8 (szInput)
Dim wch, uch, szRet
Dim x
Dim nAsc, nAsc2, nAsc3
'Exit the function if the input parameter is null.
If szInput = "" Then
ToUTF8 = szInput
Exit Function
End If
'Start Conversion
For x = 1 To Len (szInput)
'Use the mid function to split the GB encoded text
Wch = Mid (szInput, x, 1)
'Use the ascW function to return the Unicode character code of each GB encoded text
'Note: the asc function returns the ANSI character code. Note the difference.
NAsc = AscW (wch)
If nAsc <0 Then nAsc = nAsc ++ 65536

If (nAsc And & HFF80) = 0 Then
SzRet = szRet & wch
Else
If (nAsc And & HF000) = 0 Then
Uch = "%" & Hex (nAsc \ 2 ^ 6) Or & HC0) & Hex (nAsc And & H3F Or & H80)
SzRet = szRet & uch
Else
'Unicode character codes of GB encoded text use a three-byte template Between 0800-FFFF
Uch = "%" & Hex (nAsc \ 2 ^ 12) Or & HE0) & "% "&_
Hex (nAsc \ 2 ^ 6) And & H3F Or & H80) & "% "&_
Hex (nAsc And & H3F Or & H80)
SzRet = szRet & uch
End If
End If
Next

ToUTF8 = szRet
End Function

3. Convert 'gb to unicode-convert the GB-encoded text to the unicode-encoded textCopy codeThe Code is as follows: function chinese2unicode (Str)
Dim I
Dim Str_one
Dim Str_unicode
If (isnull (Str) then
Exit function
End if
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

4. 'url DecodingCopy codeThe Code is as follows: Function URLDecode (enStr)
Dim deStr
Dim c, I, v
DeStr = ""
For I = 1 to len (enStr)
C = Mid (enStr, I, 1)
If c = "%" then
V = eval ("& h" + Mid (enStr, I + 1, 2 ))
If v <128 then
DeStr = deStr & chr (v)
I = I + 2
Else
If isvalidhex (mid (enstr, I, 3) then
If isvalidhex (mid (enstr, I + 3, 3) then
V = eval ("& h" + Mid (enStr, I + 1, 2) + Mid (enStr, I + 4, 2 ))
DeStr = deStr & chr (v)
I = I + 5
Else
V = eval ("& h" + Mid (enStr, I + 1, 2) + cstr (hex (asc (Mid (enStr, I + 3, 1 )))))
DeStr = deStr & chr (v)
I = I + 3
End if
Else
Destr = destr & c
End if
End if
Else
If c = "+" then
DeStr = deStr &""
Else
DeStr = deStr & c
End if
End if
Next
URLDecode = deStr
End function

'Determine whether the hexadecimal code is valid
Function isvalidhex (str)
Dim c
Isvalidhex = true
Str = ucase (str)
If len (str) <> 3 then isvalidhex = false: exit function
If left (str, 1) <> "%" then isvalidhex = false: exit function
C = mid (str, 2, 1)
If not (c> = "0") and (c <= "9") or (c> = "") and (c <= "Z") then isvalidhex = false: exit function
C = mid (str, 3,1)
If not (c> = "0") and (c <= "9") or (c> = "") and (c <= "Z") then isvalidhex = false: exit function
End function
%>

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.