Code for converting gb2312 characters and Location Codes Using ASP

Source: Internet
Author: User

Study the encoding and find out the relationship between gb2312 encoding and location code. Program .
Search, it seems that no one writes, so it is sent here.
Original starter:
Http://bbs.blueidea.com
Http://mytju.com/classcode/
Any reprint and use.
1. Brief Introduction
(1) gb2312 standard definition is actually a location code.
There are 94 rows and 94 columns. The row is the Partition Number and the column is the location number.
For example, if the "ah" area code is 16 and the location code is 01, its location code is 1601.
(2) Each character consists of a zone number and a location number, which occupies two bytes.
Each byte is 01-94, which is in conflict with the Communication Controller 0-31,
Therefore, add the area code and location code 32 to avoid conflict.
(3) from top to bottom, each byte is 33-126, which is in conflict with ASCII code 0-127,
Therefore, the highest position is 1, that is, add 128 to avoid conflict.
Therefore, in the end, each byte is 161-254.
2. Implementation
The principle is very simple. It can be implemented by adding, subtraction, and subtraction.
Directly post the completed functions here.

CopyCode The Code is as follows: '---- function for obtaining the location code ---------------------
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. This encoding is in the correct order and does not have to be exchanged between high and low bits.
Shigh = left (shex, 2) 'gets the encoding height, such as B0.
Slow = right (shex, 2) 'gets the encoding low level, such as A1.
'Gb2312 code range is & ha1a1 -- & hfefe, each byte is between the A1-FE.
If not (shweigh> = "A1" and shweigh <= "Fe") then
Chartoqwm = ""
Exit Function
End if
If not (slow> = "A1" and slow <= "Fe") then
Chartoqwm = ""
Exit Function
End if
The 'gb swap code only uses 7 bits, and the high position is 1, that is, the inner code. In turn, the switch code can be obtained from the high position 0.
Ilow = clng ("& H" & slow)-128
Ihigh = clng ("& H" & shigh)-128
The 'location code conflicts with the control code 0-31, so after 32 is added, it is the exchange code. In turn, subtract 32.
Ilow = iLow-32
Ihigh = iHigh-32
'OK, the location code is 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
'---- Function for getting Characters Based on 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
'-- The four digits must be numbers.
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 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
'------------- Check completed ------------------
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

Usage:
Bytes -----------------------------------------------------------------------------------------------------Copy codeThe Code is 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
Bytes -----------------------------------------------------------------------------------------------------
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

.......
Bytes -----------------------------------------------------------------------------------------------------
3. Online use
Http://www.mytju.com/classcode/tools/quweima.asp
Go to the above URL and check it 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.