ASP UTF-8頁(yè)面亂碼+GB2312轉(zhuǎn)UTF-8 +生成UTF-8格式的文件(編碼)第1/2頁(yè)
更新時(shí)間:2008年10月11日 09:39:00 作者:
ASP動(dòng)態(tài)網(wǎng)頁(yè)下UTF-8頁(yè)面亂碼+GB2312轉(zhuǎn)UTF-8 +生成UTF-8格式的文件(編碼)
最好的方法:
先說(shuō)一下基本的東西:
<%@ codepage=65001%>UTF-8
<%@ codepage=936%>簡(jiǎn)體中文
<%@ codepage=950%>繁體中文
<%@ codepage=437 %>美國(guó)/加拿大英語(yǔ)
<%@ codepage=932 %>日文
<%@ codepage=949 %>韓文
<%@ codepage=866 %>俄文
codepage指定了IIS按什么編碼讀取傳遞過(guò)來(lái)的串串(表單提交,地址欄傳遞等)。
出亂碼的原因也就是網(wǎng)站要整合的時(shí)候模塊編碼不相同引起的。
就像我的博客相同,整合的時(shí)候都會(huì)出這個(gè)問(wèn)題,因?yàn)锽LOG是Utf-8的,
近來(lái)很多網(wǎng)友都在為這個(gè)問(wèn)題咨詢,我嘗試了很多種方法。
最方便的方法如下:
不要轉(zhuǎn)換任何模塊網(wǎng)頁(yè)的編碼該utf-8的還是utf-8,該Gb22312的還是Gb2312
在Utf-8模塊的包文檔(如conn.asp,但是要注意conn.asp必須是在第一行調(diào)用)最前面加上
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%Session.CodePage=65001%>
在GB2312模塊的包文檔最前面加上
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%Session.CodePage=936%>
其他編碼的類推。
ASP中漢字與UTF-8的互相轉(zhuǎn)換
'=============漢字轉(zhuǎn)換為UTF-8==================
function chinese2unicode(Str)
for i=1 to len(Str)
Str_one=Mid(Str,i,1)
Str_unicode=Str_unicode&chr(38)
Str_unicode=Str_unicode&chr(35)
Str_unicode=Str_unicode&chr(120)
Str_unicode=Str_unicode& Hex(ascw(Str_one))
Str_unicode=Str_unicode&chr(59)
next
chinese2unicode = Str_unicode
end function
'=============UTF-8轉(zhuǎn)換為漢字==================
function UTF2GB(UTFStr)
for Dig=1 to len(UTFStr)
if mid(UTFStr,Dig,1)="%" then
if len(UTFStr) >= Dig+8 then
GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9))
Dig=Dig+8
else
GBStr=GBStr & mid(UTFStr,Dig,1)
end if
else
GBStr=GBStr & mid(UTFStr,Dig,1)
end if
next
UTF2GB=GBStr
end function
function ConvChinese(x)
A=split(mid(x,2),"%")
i=0
j=0
for i=0 to ubound(A)
A(i)=c16to2(A(i))
next
for i=0 to ubound(A)-1
DigS=instr(A(i),"0")
Unicode=""
for j=1 to DigS-1
if j=1 then
A(i)=right(A(i),len(A(i))-DigS)
Unicode=Unicode & A(i)
else
i=i+1
A(i)=right(A(i),len(A(i))-2)
Unicode=Unicode & A(i)
end if
next
if len(c2to16(Unicode))=4 then
ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode)))
else
ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode)))
end if
next
end function
function c2to16(x)
i=1
for i=1 to len(x) step 4
c2to16=c2to16 & hex(c2to10(mid(x,i,4)))
next
end function
function c2to10(x)
c2to10=0
if x="0" then exit function
i=0
for i= 0 to len(x) -1
if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i)
next
end function
function c16to2(x)
i=0
for i=1 to len(trim(x))
tempstr= c10to2(cint(int("&h" & mid(x,i,1))))
do while len(tempstr)<4
tempstr="0" & tempstr
loop
c16to2=c16to2 & tempstr
next
end function
function c10to2(x)
mysign=sgn(x)
x=abs(x)
DigS=1
do
if x<2^DigS then
exit do
else
DigS=DigS+1
end if
loop
tempnum=x
i=0
for i=DigS to 1 step-1
if tempnum>=2^(i-1) then
tempnum=tempnum-2^(i-1)
c10to2=c10to2 & "1"
else
c10to2=c10to2 & "0"
end if
next
if mysign=-1 then c10to2="-" & c10to2
end function
GB2312轉(zhuǎn)UTF-8
'個(gè)人代碼風(fēng)格注釋(變量名中第一個(gè)小寫(xiě)字母表表示變量類型)
'i:為Integer型;
's:為String;
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 < 128) Then
sResult = sResult & 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 = ChrB(&H80 + (iHexNum And &H3F)) & sResult
iHexNum = iHexNum \ &H40
sResult = ChrB(&HE0 + (iHexNum And &HF)) & sResult
End If
U2UTF8 = sResult
End Function
Function GB2UTF(Byval a_sStr)
Dim sGB,sResult,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 iUnicode<0 Then
iUnicode = iUnicode + 65536
End If
Else
iUnicode = iTemp
End If
sResult = sResult & U2UTF8(iUnicode)
Next
GB2UTF = sResult
End Function
'調(diào)用方法
Response.BinaryWrite(GB2UTF("中國(guó)人"))
先說(shuō)一下基本的東西:
<%@ codepage=65001%>UTF-8
<%@ codepage=936%>簡(jiǎn)體中文
<%@ codepage=950%>繁體中文
<%@ codepage=437 %>美國(guó)/加拿大英語(yǔ)
<%@ codepage=932 %>日文
<%@ codepage=949 %>韓文
<%@ codepage=866 %>俄文
codepage指定了IIS按什么編碼讀取傳遞過(guò)來(lái)的串串(表單提交,地址欄傳遞等)。
出亂碼的原因也就是網(wǎng)站要整合的時(shí)候模塊編碼不相同引起的。
就像我的博客相同,整合的時(shí)候都會(huì)出這個(gè)問(wèn)題,因?yàn)锽LOG是Utf-8的,
近來(lái)很多網(wǎng)友都在為這個(gè)問(wèn)題咨詢,我嘗試了很多種方法。
最方便的方法如下:
不要轉(zhuǎn)換任何模塊網(wǎng)頁(yè)的編碼該utf-8的還是utf-8,該Gb22312的還是Gb2312
在Utf-8模塊的包文檔(如conn.asp,但是要注意conn.asp必須是在第一行調(diào)用)最前面加上
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%Session.CodePage=65001%>
在GB2312模塊的包文檔最前面加上
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%Session.CodePage=936%>
其他編碼的類推。
ASP中漢字與UTF-8的互相轉(zhuǎn)換
'=============漢字轉(zhuǎn)換為UTF-8==================
function chinese2unicode(Str)
for i=1 to len(Str)
Str_one=Mid(Str,i,1)
Str_unicode=Str_unicode&chr(38)
Str_unicode=Str_unicode&chr(35)
Str_unicode=Str_unicode&chr(120)
Str_unicode=Str_unicode& Hex(ascw(Str_one))
Str_unicode=Str_unicode&chr(59)
next
chinese2unicode = Str_unicode
end function
'=============UTF-8轉(zhuǎn)換為漢字==================
function UTF2GB(UTFStr)
for Dig=1 to len(UTFStr)
if mid(UTFStr,Dig,1)="%" then
if len(UTFStr) >= Dig+8 then
GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9))
Dig=Dig+8
else
GBStr=GBStr & mid(UTFStr,Dig,1)
end if
else
GBStr=GBStr & mid(UTFStr,Dig,1)
end if
next
UTF2GB=GBStr
end function
function ConvChinese(x)
A=split(mid(x,2),"%")
i=0
j=0
for i=0 to ubound(A)
A(i)=c16to2(A(i))
next
for i=0 to ubound(A)-1
DigS=instr(A(i),"0")
Unicode=""
for j=1 to DigS-1
if j=1 then
A(i)=right(A(i),len(A(i))-DigS)
Unicode=Unicode & A(i)
else
i=i+1
A(i)=right(A(i),len(A(i))-2)
Unicode=Unicode & A(i)
end if
next
if len(c2to16(Unicode))=4 then
ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode)))
else
ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode)))
end if
next
end function
function c2to16(x)
i=1
for i=1 to len(x) step 4
c2to16=c2to16 & hex(c2to10(mid(x,i,4)))
next
end function
function c2to10(x)
c2to10=0
if x="0" then exit function
i=0
for i= 0 to len(x) -1
if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i)
next
end function
function c16to2(x)
i=0
for i=1 to len(trim(x))
tempstr= c10to2(cint(int("&h" & mid(x,i,1))))
do while len(tempstr)<4
tempstr="0" & tempstr
loop
c16to2=c16to2 & tempstr
next
end function
function c10to2(x)
mysign=sgn(x)
x=abs(x)
DigS=1
do
if x<2^DigS then
exit do
else
DigS=DigS+1
end if
loop
tempnum=x
i=0
for i=DigS to 1 step-1
if tempnum>=2^(i-1) then
tempnum=tempnum-2^(i-1)
c10to2=c10to2 & "1"
else
c10to2=c10to2 & "0"
end if
next
if mysign=-1 then c10to2="-" & c10to2
end function
GB2312轉(zhuǎn)UTF-8
'個(gè)人代碼風(fēng)格注釋(變量名中第一個(gè)小寫(xiě)字母表表示變量類型)
'i:為Integer型;
's:為String;
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 < 128) Then
sResult = sResult & 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 = ChrB(&H80 + (iHexNum And &H3F)) & sResult
iHexNum = iHexNum \ &H40
sResult = ChrB(&HE0 + (iHexNum And &HF)) & sResult
End If
U2UTF8 = sResult
End Function
Function GB2UTF(Byval a_sStr)
Dim sGB,sResult,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 iUnicode<0 Then
iUnicode = iUnicode + 65536
End If
Else
iUnicode = iTemp
End If
sResult = sResult & U2UTF8(iUnicode)
Next
GB2UTF = sResult
End Function
'調(diào)用方法
Response.BinaryWrite(GB2UTF("中國(guó)人"))
您可能感興趣的文章:
- 將字符串轉(zhuǎn)換成gb2312或者utf-8編碼的參數(shù)(js版)
- php實(shí)現(xiàn)utf-8和GB2312編碼相互轉(zhuǎn)換函數(shù)代碼
- UTF-8 GBK UTF8 GB2312 之間的區(qū)別和關(guān)系介紹
- PHP字符編碼問(wèn)題之GB2312 VS UTF-8解決方法
- VBS實(shí)現(xiàn)GB2312,UTF-8,Unicode,BIG5編碼轉(zhuǎn)換工具
- PHP 解決utf-8和gb2312編碼轉(zhuǎn)換問(wèn)題
- unicode utf-8 gb18030 gb2312 gbk各種編碼對(duì)比
- ASP中Utf-8與Gb2312編碼轉(zhuǎn)換亂碼問(wèn)題的解決方法 頁(yè)面編碼聲明
- MSSQL轉(zhuǎn)MYSQL,gb2312轉(zhuǎn)utf-8無(wú)亂碼解決方法
- PHP iconv 解決utf-8和gb2312編碼轉(zhuǎn)換問(wèn)題
- 用VBS實(shí)現(xiàn)的批量gb2312轉(zhuǎn)utf-8,支持拖動(dòng)
- 用javascript實(shí)現(xiàn)gb2312轉(zhuǎn)utf-8的腳本
- [轉(zhuǎn)]ASP實(shí)現(xiàn)關(guān)鍵詞獲取(各搜索引擎,GB2312及UTF-8)
- UTF-8轉(zhuǎn)GB2312函數(shù)
- utf-8編碼轉(zhuǎn)換成gb2312
- 將編碼從GB2312轉(zhuǎn)成UTF-8的方法匯總(從前臺(tái)、程序、數(shù)據(jù)庫(kù))
相關(guān)文章
asp UTF-8 亂碼問(wèn)題的解決方法小結(jié)
這里整理下asp頁(yè)面下出現(xiàn)亂碼的問(wèn)題,好多ajax讀取的數(shù)據(jù)出現(xiàn)亂碼的解決方法,asp.net,php等語(yǔ)言的亂碼,解決原理都是一樣的。2007-12-12ASP利用Google實(shí)現(xiàn)在線翻譯功能
ASP利用Google實(shí)現(xiàn)在線翻譯功能...2006-09-09關(guān)于網(wǎng)站文件自動(dòng)備份程序的一點(diǎn)思考
關(guān)于網(wǎng)站文件自動(dòng)備份程序的一點(diǎn)思考...2006-12-12在asp中使用js的encodeURIComponent方法
encodeURIComponent 方法返回一個(gè)已編碼的 URI。如果您將編碼結(jié)果傳遞給 decodeURIComponent,那么將返回初始的字符串2012-03-03ASP MSSQL存儲(chǔ)過(guò)程的實(shí)現(xiàn)小例
很多朋友學(xué)ASP以后,就只會(huì)簡(jiǎn)單的SELECT、UPDATE、DELETE幾個(gè)基本命令。這樣在讀取或者存入數(shù)據(jù)的時(shí)候,大大降低的系統(tǒng)運(yùn)行的速度。2009-02-02