Gb2312 turn utf-8 (VBS+JS)

Source: Internet
Author: User
Tags chr
Js

Yesterday I looked at the code of Cocoon counter, and found that it was in VBScript, and that it took morning time to study it, or was made dizzy

His VB conversion function is this:


Function Decodeansi (s)
Dim I, STMP, Sresult, STMP1
Sresult = ""
For I=1 to Len (s)
If Mid (s,i,1) = "%" Then
STMP = "&h" & Mid (s,i+1,2)
If IsNumeric (STMP) Then
If CInt (STMP) =0 Then
i = i + 2
ElseIf CInt (STMP) >0 and CInt (STMP) <128
Sresult = Sresult & Chr (STMP)
i = i + 2
Else
If Mid (s,i+3,1) = "%" Then
STMP1 = "&h" & Mid (s,i+4,2)
If IsNumeric (STMP1) Then
Sresult = Sresult & Chr (CInt (STMP) *16*16 + CInt (STMP1))
i = i + 5
End If
Else
Sresult = Sresult & Chr (STMP)
i = i + 2
End If
End If
Else
Sresult = Sresult & Mid (s,i,1)
End If
Else
Sresult = Sresult & Mid (s,i,1)
End If
Next
Decodeansi = Sresult
End Function

That is, use the Chr () function to convert the 10 ANSI character code into text. The text itself should be Unicode, that is, the VBS automatically completes the GB-UTF conversion, and here are some of the data I tested:
Test code: (need to add the above code to the front)

<script Runat=server language=javascript>
Response.Write ("<br/>strx = Chr (54992):");
Response.Write (Strx);
Response.Write ("<br/>strx.charcodeat (0):");
Response.Write (strx.charcodeat (0));
Response.Write ("<br/>\" ". charCodeAt (0):");
Response.Write ("Medium". charCodeAt (0));
Response.Write ("<br/>escape (Strx):");
Response.Write (Escape (Strx));
Response.Write ("<br/>encodeuri (Strx):");
Response.Write (encodeURI (Strx));
Response.Write ("<br/>escape"): ");
Response.Write (Escape ("Middle"));
Response.Write ("<br/>string.fromcharcode (20013):");
Response.Write (String.fromCharCode (20013));
</SCRIPT>


Adjust the file storage format separately, codepage,charset the result:

File is in ANSI format:
codepage=936:
Response.Charset = "gb2312";
Strx = Chr (54992)
Strx: Middle
Strx.charcodeat (0): 20013
"Medium". charCodeAt (0): 20013
Escape (Strx):%u4e2d
encodeURI (Strx):%e4%b8%ad
Escape ("Medium"):%u4e2d
String.fromCharCode (20013): Medium

Response.Charset = "Utf-8";
Strx = Chr (54992)
Strx:֐
Strx.charcodeat (0): 20013
"֐". charCodeAt (0): 20013
Escape (Strx):%u4e2d
encodeURI (Strx):%e4%b8%ad
Escape ("֐"):%u4e2d
String.fromCharCode (20013): ֐

CODEPAGE=65001:
Response.Charset = "gb2312";
Strx = Chr (54992)
Strx: Juan
Strx.charcodeat (0): 20013
"". charCodeAt (0): -1. #IND
Escape (Strx):%u4e2d
encodeURI (Strx):%e4%b8%ad
Escape (""):
String.fromCharCode (20013): Juan

Response.Charset = "Utf-8";
Strx = Chr (54992)
Strx:㝤
Strx.charcodeat (0): 14180
"". charCodeAt (0): -1. #IND
Escape (Strx):%u3764
encodeURI (Strx):%e3%9d%a4
Escape (""):
String.fromCharCode (20013): Medium

File is in utf-8 format:
CODEPAGE=65001:
Response.Charset = "gb2312";
Strx = Chr (54992)
Strx: Juan
Strx.charcodeat (0): 20013
"Juan?" he said. charCodeAt (0): 20013
Escape (Strx):%u4e2d
encodeURI (Strx):%e4%b8%ad
Escape ("trickle?"):%u4e2d
String.fromCharCode (20013): Juan

Response.Charset = "Utf-8";
Strx = Chr (54992)
Strx: Middle
Strx.charcodeat (0): 20013
"Medium". charCodeAt (0): 20013
Escape (Strx):%u4e2d
encodeURI (Strx):%e4%b8%ad
Escape ("Medium"):%u4e2d
String.fromCharCode (20013): Medium

codepage=936:
Active Server Pages error ' ASP 0245 '
Mixed use of code page values
/referer_alapha/test2.asp, Line 1
The specified @CODEPAGE value is inconsistent with the value of the file containing the CODEPAGE or the file's saved format.


Haha, are you looking dizzy? I'm dizzy too, I can't figure out why the format of the file store is related to the function of Chr (54992), and String.fromCharCode (20013) can get the correct result (part Fourth of the test). Probably a VBS inside the logic is too confusing.
In any case, with this method, gb2312 turn utf-8 much simpler.



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.