About the online popular ASP version GB2312 to UTF-8 function error correction!

Source: Internet
Author: User
Tags trim truncated

The same article I have sent to the blue ideal: http://bbs.blueidea.com/thread-3060231-1-1.html

Original function Source: http://www.blueidea.com/tech/program/2006/3538.asp

Thank the author for sending out this function, but found that this function has a small error:

If the string that needs to be converted is a Chinese character, no problem. However, if there is a number or letter in the middle of the time will be truncated, and not get the correct results.

For example: Gb2utf ("Chinese") can get the correct conversion.

But: Gb2utf ("Chinese 3chinese!") You'll get "Chinese 5".

Not only is truncated, but the first number also does not correspond.

It's a small mistake, but it can be misleading for newbies.

In order to stop everyone from wasting time looking for errors, the wrong version and the correct version are pasted so that learners can compare them.

there is an incorrect version of the online epidemic:

Function U2utf8 (Byval a_inum) Dim Sresult,sutf8 Dim Itemp,ihexnum,i Ihexnum = Trim (a_inum) If ihexnum = "" Then Exit Function End If Sresult = "" if (Ihexnum < +) Then Sresult = Sresult & iHe Xnum ElseIf (Ihexnum < 2048) Then Sresult = ChrB (&h80 + (Ihexnum and &h3f)) Ihexnum = Ihexn 
        Um \ &h40 sresult = ChrB (&hc0 + (Ihexnum and &h1f)) & Sresult ElseIf (Ihexnum < 65536) Then Sresult = ChrB (&h80 + (Ihexnum and &h3f)) Ihexnum = ihexnum \ &h40 sresult = ChrB (&am P H80 + (Ihexnum and &h3f)) & sresult Ihexnum = ihexnum \ &h40 sresult = ChrB (&he0 + (ihexnu M and &AMP;HF)) & Sresult end If U2utf8 = Sresult End Function function Gb2utf (Byval a_sstr) Dim sgb,sres Ult,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 iunic
        Ode<0 then Iunicode = Iunicode + 65536 End If Else iunicode = iTemp End If Sresult = Sresult & U2utf8 (iunicode) Next Gb2utf = Sresult End Function Response.Write
 (Gb2utf ("Ghost Butterfly")) ' can get the correct result Response.Write (Gb2utf ("Pluto 308641836")) ' Get: Hades Butterfly 5, the result is wrong.


modified version:

Function U2utf8 (Byval a_inum) Dim Sresult,sutf8 Dim Itemp,ihexnum,i Ihexnum = Trim (a_inum) If ihexnum = "" Then Exit Function End If Sresult = "" if (Ihexnum < +) Then Sresult = Sresult & Chr  B (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 = Ch  RB (&h80 + (Ihexnum and &h3f)) & sresult Ihexnum = ihexnum \ &h40 sresult = ChrB (&he0 + (Ihexnum and &AMP;HF)) & Sresult End If U2utf8 = Sresult End Function function Gb2utf (Byval a_sstr) Dim sgb,sresult,stemp Di
   M 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 = i Temp end If Sresult = Sresult & U2utf8 (iunicode) Next Gb2utf = Sresult End Function RESPONSE.W

 Rite (Gb2utf ("Ghost Butterfly")) ' can get the correct result Response.Write (Gb2utf ("Pluto 308641836") ' Get: 308641836, the result is no longer truncated!



This code is for study only.
Posted By: Hades May 11, 2012

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.