The code-application technique of ASP to realize GB2312 character and location code's mutual conversion

Source: Internet
Author: User
Study the code, learned that the GB2312 code and location code, after the attempt to get this program.
Search, no one seems to write, so hair here.
Original starter:
Http://bbs.blueidea.com
http://mytju.com/classcode/
Arbitrarily reproduced, arbitrary use.
1. Briefly
(1) The definition of GB2312 standard, in fact, is the location code.
A total of 94 rows, 94 columns, the line is the area code, the column is a bit number.
such as "Ah" word area code is 16, the number is 01, its location code is 1601.
(2) Each character is composed of an area code + bit number, which occupies a total of two bytes.
Each byte is 01-94, conflicting with the traffic control character 0-31,
So, add the area code and the bit number 32 to avoid the conflict.
(3) by the top, each byte is 33-126, and the ASCII encoding 0-127 conflicts,
So the top position is 1, plus 128, to avoid conflict.
So, eventually, each byte is 161-254.
2. Realize
The principle is very simple, the reduction of Gaga can be achieved.
Post the function I finished directly here.

Copy Code code as follows:

'----the function of obtaining location codes---------------------
Function chartoqwm (ByVal str)
Dim Shex,shigh,slow,ilow,ihigh,sresult
Shex=hex (ASC (str)) ' Gets the encoding of the character's inner code, such as B0A1, which is in the correct order and does not have to be exchanged for high-low bits.
Shigh=left (shex,2) ' Gets the encoded high, such as B0.
Slow=right (shex,2) ' gets coded low, such as A1.
' GB2312 within the range of &ha1a1--&hfefe, each byte is between A1-fe.
If not (shigh>= "A1" and shigh<= "FE") Then
Chartoqwm= ""
Exit Function
End If
If not (slow>= "A1" and slow<= "FE") Then
Chartoqwm= ""
Exit Function
End If
The GB Interchange code uses only 7 digits, high position 1, which is the inner code. The flip side is a high position of 0, which can get the swap code.
ILOW=CLNG ("&h" & Slow)-128
IHIGH=CLNG ("&h" & Shigh)-128
' Location Code and control Code 0-31 conflict, so add 32, that is, Exchange code. Minus 32 in turn.
Ilow=ilow-32
Ihigh=ihigh-32
' OK, Location code has been obtained.
Sresult= ""
If Ihigh<10 Then
Sresult = Sresult & "0" & Cstr (Ihigh)
Else
Sresult = Sresult & Cstr (Ihigh)
End If
If Ilow<10 Then
Sresult = Sresult & "0" & Cstr (Ilow)
Else
Sresult = Sresult & Cstr (Ilow)
End If
Chartoqwm=sresult
End Function
'----Gets the function of the character according to the location Code---------------------
Function Qwmtochar (byVal str,byval DOCHECKFLG)
Dim Shex,shigh,slow,ilow,ihigh,sresult
'-------------check the input format--------------
If Docheckflg Then
If Len (str) <>4 Then
Qwmtochar= ""
Exit Function
End If
'-4-digit must be a number
Dim i,iasc
For I=1 to 4
IASC=ASC (Mid (str,i,1))
If not (iasc>=&h30 and iasc<=&h39) then
Qwmtochar= ""
Exit Function
End If
Next
'--the area code, the number must be between 01-94
IHIGH=CLNG (Left (str,2))
ILOW=CLNG (Right (str,2))
If not (Ihigh>=1 and ihigh<=94) then
Qwmtochar= ""
Exit Function
End If
If not (Ilow>=1 and ilow<=94) then
Qwmtochar= ""
Exit Function
End If
End If
'-------------checked------------------
IHIGH=CLNG (Left (str,2))
ILOW=CLNG (Right (str,2))
Ihigh=ihigh + 32 + 128
Ilow=ilow + 32 + 128
Shex=hex (Ihigh) & Hex (Ilow)
QWMTOCHAR=CHR ("&h" & ShEx)
End Function

How to use:
-----------------------------------------------------------------------------------------------------
Copy Code code as follows:

Dim I,str,schar
Str= "Wahaha"
For I=1 to Len (str)
Schar=mid (str,i,1)
Response.Write Schar & ":" & Chartoqwm (Schar) & "<br>"
Next
-----------------------------------------------------------------------------------------------------
Dim str
Str= "1601|1602|1603}"
If InStr (str, "|") >0 Then
Dim s,schararray,i
Schararray=split (str, "|")
For i=0 to Ubound (Schararray)
S=s & Qwmtochar (Trim (Schararray (i)), True)
Next
Str=s
Else
Str=qwmtochar (Str,true)
End If
.......
-----------------------------------------------------------------------------------------------------
3. Online use
Http://www.mytju.com/classcode/tools/quweima.asp
Access to the above Web site can be accessed online.
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.