ASP 二進位與字串互轉, 另類完美解決方案, adodb.stream 實現 By shawl.qiu

來源:互聯網
上載者:User

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

  1. <%
  2.         private function bTsAscii(bin)
  3.         '二進位轉為 string (bmp|gif|png|jpg)
  4.             dim i, iByt, sByt, bLen:bLen=lenB(bin)
  5.             for i=1 to bLen
  6.                 sByt=midB(bin,i,1):iByt=ascB(sByt)
  7.                 if iByt<128 then
  8.                     bTsAscii=bTsAscii&chr(iByt)
  9.                 else:i=i+1
  10.                     if i<=bLen then bTsAscii=bTsAscii&chr(ascW(sByt&sByt))
  11.                 end if
  12.             next 'shawl.qiu code'
  13.         end function
  14. %>

2. gb2312 字元集 二進位流轉字串函數

    linenum

  1. <%  
  2.         private function bTsGb2312(bin)
  3.         '二進位轉為 string | gb2312 編碼
  4.             dim i, iByt, sByt, bLen:bLen=lenB(bin)
  5.             for i=1 to bLen
  6.                 sByt=midB(bin,i,1):iByt=ascB(sByt)
  7.                 if iByt<128 then
  8.                     bTsGb2312=bTsGb2312&chr(iByt)
  9.                 else
  10.                     bTsGb2312=bTsGb2312&chr(ascW(midB(bin,i+1,1)&sByt))
  11.                     i=i+1
  12.                 end if
  13.             next 'shawl.qiu code'
  14.         end function
  15. %>

3. adodb.stream 字串轉二進位流函數 與 二進位流轉字串函數 及示範操作

    linenum

  1. <%
  2.     dim str:str="adodb.stream 實現 二進位與字串的互轉 By Shawl.qiu"
  3.     
  4.         response.write "字串轉二進位 response.binaryWrite sTb(str, ""utf-8""):<br/>"
  5.         response.binaryWrite sTb(str, "utf-8")
  6.         
  7.         response.write "<p/>二進位轉字串 response.write bTs(midB(sTb(str, ""utf-8""),1),""utf-8"")<br/>"
  8.         response.write bTs(midB(sTb(str, "utf-8"),1),"utf-8")
  9.         
  10.     function sTb(str, charSet)
  11.     '''''''''''''''''''''''''''''
  12.     ' 字串轉二進位函數 By shawl.qiu
  13.     '   http://blog.csdn.net/btbtd
  14.     ''''''''''''''''
  15.     ' 參數說明
  16.     ''''''''''
  17.     ' str: 要轉換成二進位的字串
  18.     ' charSet: 字串預設編碼集, 如不指定, 則預設為 gb2312
  19.     ''''''''''
  20.     ' sample call: response.binaryWrite sTb(str, "utf-8")
  21.     '''''''''''''''''''''''''''''
  22.         dim stm_ 
  23.         set stm_=createObject("adodb.stream")
  24.             with stm_
  25.                 .type=2 
  26.                 if charSet<>"" then
  27.                     .charSet=charSet
  28.                 else
  29.                     .charSet="gb2312"
  30.                 end if
  31.                 .open
  32.                 .writeText str
  33.                 .Position = 0
  34.                 .type=1
  35.                 sTb=.Read
  36.                 .close
  37.             end with 'shawl.qiu code'
  38.         set stm_=nothing
  39.     end function
  40.     
  41.     function bTs(str, charSet)
  42.     '''''''''''''''''''''''''''''
  43.     ' 二進位轉字串函數 By shawl.qiu
  44.     '   http://blog.csdn.net/btbtd
  45.     ''''''''''''''''
  46.     ' 參數說明
  47.     ''''''''''
  48.     ' str: 要轉換成字串的位元據
  49.     ' charSet: 字串預設編碼集, 如不指定, 則預設為 gb2312
  50.     ''''''''''
  51.     ' sample call: response.write bTs(midB(sTb(str, "utf-8"),1),"utf-8")
  52.     '''''''''''''''''''''''''''''
  53.     ' 注意: 二進位字串必須先用 midB(binaryString,1) 讀取(可自定讀取長度).
  54.     '''''''''''''''''''''''''''''
  55.         dim stm_ 
  56.         set stm_=createObject("adodb.stream")
  57.             with stm_
  58.                 .type=2 
  59.                 .open
  60.                 .writeText str
  61.                 .Position = 0
  62.                 if charSet<>"" then
  63.                     .CharSet = charSet
  64.                 else 
  65.                     .CharSet = "gb2312"
  66.                 end if
  67.                     bTs=.ReadText
  68.                 .close
  69.             end with 'shawl.qiu code'
  70.         set stm_=nothing
  71.     end function
  72. %>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.