ASP 二進位與字串互轉, 另類完美解決方案, adodb.stream 實現 By shawl.qiu
摘要:
本文實現了使用 ASP 內建組件 adodb.stream 進行任何字元集編碼的 二進位轉字串 與 字串轉二進位 的互轉操作.
說明:
要實現二進位流轉字串操作, 可以說很容易, 容易轉換的是 us-ascii 字元集的二進位流轉字串, 到處是這種標準的資料.
有點難度的是 gb2312字元集 二進位流轉字串, gb2312字元集 每個中文佔用兩個位元組空間, 這很容易計算, 因為有相關資料.
高難度的是 unicode字元集 二進位流轉字串, unicode字元集 每個中文佔用三個位元組的空間, 我翻遍了相關的權威網站, 沒有一個網站有 unicode字元集 與 二進位流 的相關介紹, 所以說是高難度. 若有相關標準文檔, 幹什麼都很輕鬆, 前提是芽菜文要過關.
最後, 經過俺差不多十小時的奮鬥, 終於發現了使用 asp adodb.stream 內建組件 可以輕鬆的搞定任何編碼的二進位流與字串的互轉. :)
目錄:
1. ascii 字元集 二進位流轉字串函數
2. gb2312 字元集 二進位流轉字串函數
3. adodb.stream 字串轉二進位流函數 與 二進位流轉字串函數 及示範操作
shawl.qiu
2006-09-26
http://blog.csdn.net/btbtd
1. ascii 字元集 二進位流轉字串函數
linenum
- <%
- private function bTsAscii(bin)
- '二進位轉為 string (bmp|gif|png|jpg)
- dim i, iByt, sByt, bLen:bLen=lenB(bin)
- for i=1 to bLen
- sByt=midB(bin,i,1):iByt=ascB(sByt)
- if iByt<128 then
- bTsAscii=bTsAscii&chr(iByt)
- else:i=i+1
- if i<=bLen then bTsAscii=bTsAscii&chr(ascW(sByt&sByt))
- end if
- next 'shawl.qiu code'
- end function
- %>
2. gb2312 字元集 二進位流轉字串函數
linenum
- <%
- private function bTsGb2312(bin)
- '二進位轉為 string | gb2312 編碼
- dim i, iByt, sByt, bLen:bLen=lenB(bin)
- for i=1 to bLen
- sByt=midB(bin,i,1):iByt=ascB(sByt)
- if iByt<128 then
- bTsGb2312=bTsGb2312&chr(iByt)
- else
- bTsGb2312=bTsGb2312&chr(ascW(midB(bin,i+1,1)&sByt))
- i=i+1
- end if
- next 'shawl.qiu code'
- end function
- %>
3. adodb.stream 字串轉二進位流函數 與 二進位流轉字串函數 及示範操作
linenum
- <%
- dim str:str="adodb.stream 實現 二進位與字串的互轉 By Shawl.qiu"
-
- response.write "字串轉二進位 response.binaryWrite sTb(str, ""utf-8""):<br/>"
- response.binaryWrite sTb(str, "utf-8")
-
- response.write "<p/>二進位轉字串 response.write bTs(midB(sTb(str, ""utf-8""),1),""utf-8"")<br/>"
- response.write bTs(midB(sTb(str, "utf-8"),1),"utf-8")
-
- function sTb(str, charSet)
- '''''''''''''''''''''''''''''
- ' 字串轉二進位函數 By shawl.qiu
- ' http://blog.csdn.net/btbtd
- ''''''''''''''''
- ' 參數說明
- ''''''''''
- ' str: 要轉換成二進位的字串
- ' charSet: 字串預設編碼集, 如不指定, 則預設為 gb2312
- ''''''''''
- ' sample call: response.binaryWrite sTb(str, "utf-8")
- '''''''''''''''''''''''''''''
- dim stm_
- set stm_=createObject("adodb.stream")
- with stm_
- .type=2
- if charSet<>"" then
- .charSet=charSet
- else
- .charSet="gb2312"
- end if
- .open
- .writeText str
- .Position = 0
- .type=1
- sTb=.Read
- .close
- end with 'shawl.qiu code'
- set stm_=nothing
- end function
-
- function bTs(str, charSet)
- '''''''''''''''''''''''''''''
- ' 二進位轉字串函數 By shawl.qiu
- ' http://blog.csdn.net/btbtd
- ''''''''''''''''
- ' 參數說明
- ''''''''''
- ' str: 要轉換成字串的位元據
- ' charSet: 字串預設編碼集, 如不指定, 則預設為 gb2312
- ''''''''''
- ' sample call: response.write bTs(midB(sTb(str, "utf-8"),1),"utf-8")
- '''''''''''''''''''''''''''''
- ' 注意: 二進位字串必須先用 midB(binaryString,1) 讀取(可自定讀取長度).
- '''''''''''''''''''''''''''''
- dim stm_
- set stm_=createObject("adodb.stream")
- with stm_
- .type=2
- .open
- .writeText str
- .Position = 0
- if charSet<>"" then
- .CharSet = charSet
- else
- .CharSet = "gb2312"
- end if
- bTs=.ReadText
- .close
- end with 'shawl.qiu code'
- set stm_=nothing
- end function
- %>