研究編碼,得知GB2312編碼與區(qū)位碼的關系,嘗試之后,得此程序。
搜索,似乎沒人寫,故發(fā)此地。
原創(chuàng)首發(fā):
http://bbs.blueidea.com
http://mytju.com/classcode/
任意轉載,任意使用。
1.簡述
(1)GB2312標準的定義,其實就是區(qū)位碼。
共94行,94列,行就是區(qū)號,列就是位號。
如“啊”字區(qū)號為16,位號為01,它的區(qū)位碼就是1601。
(2)每個字符由區(qū)號+位號組成,共占兩個字節(jié)。
每個字節(jié)都是01-94,與通信控制符0-31沖突,
所以,將區(qū)號和位號分別加上32,以避免沖突。
(3)由上,每個字節(jié)是33-126,與ASCII編碼0-127沖突,
所以將最高位置為1,也就是加上128,以避免沖突。
所以,最終,每個字節(jié)為161-254。
2。實現(xiàn)
原理很簡單,加加減減即可實現(xiàn)。
直接將我完成的函數(shù)帖于此處。
復制代碼 代碼如下:
'----取得區(qū)位碼的函數(shù)---------------------
Function CharToQWM(byVal str)
dim sHex,sHigh,sLow,iLow,iHigh,sResult
sHex=Hex(Asc(str)) '取得字符的內(nèi)碼的編碼,如B0A1,此編碼是正確的順序,不必交換高低位。
sHigh=Left(sHex,2) '取得編碼的高位,如B0。
sLow=Right(sHex,2) '取得編碼的低位,如A1。
'GB2312內(nèi)碼范圍為HA1A1--HFEFE,每個字節(jié)都在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
'GB交換碼僅使用了7位,高位置1,即為內(nèi)碼。反過來就是將高位置0,可得到交換碼。
iLow=Clng("H" sLow)-128
iHigh=Clng("H" sHigh)-128
'區(qū)位碼與控制碼0-31沖突,所以加上32之后,即是交換碼。反過來減去32即可。
iLow=iLow-32
iHigh=iHigh-32
'OK,區(qū)位碼已經(jīng)得到。
sResult=""
if iHigh10 then
sResult = sResult "0" Cstr(iHigh)
else
sResult = sResult Cstr(iHigh)
end if
if iLow10 then
sResult = sResult "0" Cstr(iLow)
else
sResult = sResult Cstr(iLow)
end if
CharToQWM=sResult
End Function
'----根據(jù)區(qū)位碼得到字符的函數(shù)---------------------
Function QWMToChar(byVal str,byVal doCheckFlg)
dim sHex,sHigh,sLow,iLow,iHigh,sResult
'-------------檢查輸入格式--------------
if doCheckFlg then
if Len(str)>4 then
QWMToChar=""
Exit Function
end if
'--4位必須都是數(shù)字
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
'--區(qū)號,位號都要在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
'-------------檢查完畢------------------
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
使用方法:
-----------------------------------------------------------------------------------------------------
復制代碼 代碼如下:
dim i,str,sChar
str="娃哈哈"
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.在線使用
http://www.mytju.com/classcode/tools/quweima.asp
進入以上網(wǎng)址即可在線查閱。
您可能感興趣的文章:- PHP中實現(xiàn)漢字轉區(qū)位碼應用源碼實例解析