在ASP中,字符转二进制流可以通过使用ADODB.Stream对象实现,具体代码如下:,“
asp,Function BinaryToString(binary), Dim stream, Set stream = Server.CreateObject("ADODB.Stream"), stream.Type = 1 ' adTypeBinary, stream.Open, stream.Write binary, stream.Position = 0, stream.Type = 2 ' adTypeText, stream.Charset = "UTF-8", BinaryToString = stream.ReadText, stream.Close,End Function,
“在互联网技术领域,字符转二进制流是数据传输和存储中常见需求,本文将详细介绍如何在ASP中实现字符与二进制流之间的转换,包括具体函数实现、操作步骤及示例代码。
使用ADODB.Stream对象进行转换
ASCII字符集的转换
ASCII字符集的转换相对简单,因为每个字符只占用一个字节,以下是实现ASCII字符集转换的函数:
<% 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 end function %>
GB2312字符集的转换
GB2312字符集每个中文字符占用两个字节,以下是实现GB2312字符集转换的函数:
<% 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 end function %>
Unicode字符集的转换
Unicode字符集每个中文字符占用三个字节,以下是实现Unicode字符集转换的函数:
<% private function bTsUnicode(bin) '二进制转为 string | unicode 编码 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 bTsUnicode=bTsUnicode&chr(iByt) elseif iByt<224 then bTsUnicode=bTsUnicode&chr(ascW(midB(bin,i+1,1)&sByt)) i=i+1 elseif iByt<240 then bTsUnicode=bTsUnicode&chr(asclng(midB(bin,i+1,2)&sByt)) i=i+2 else bTsUnicode=bTsUnicode&chr(aschx(midB(bin,i+1,3)&sByt)) i=i+3 end if next end function %>
字符串转二进制流函数与二进制流转字符串函数及演示操作
字符串转二进制流函数sTb
<% function sTb(str, charSet) ' 字符串转二进制函数 By shawl.qiu ' 参数说明 ' str: 要转换成二进制的字符串 ' charSet: 字符串默认编码集 如不指定, 则默认为 gb2312 ' sample call: response.binaryWrite sTb(str, "utf-8") 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' end function %>
二进制流转字符串函数bTs
<% function bTs(str, charSet) ' 二进制转字符串函数 By shawl.qiu ' 参数说明 ' 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' end function %>
相关问题与解答栏目
Q1: 如何将字符串转换为二进制流并输出到网页?
A1: 可以使用response.binaryWrite
方法将二进制流输出到网页。
<% dim str:str="adodb.stream 实现 二进制与字符串的互转 By Shawl.qiu" response.write "字符串转二进制 response.binaryWrite sTb(str, ""utf-8""):<br/>" response.binaryWrite sTb(str, "utf-8") %>
Q2: 如何将二进制流转换回字符串并在网页上显示?
A2: 可以使用response.write
方法将转换后的字符串输出到网页。
<% 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") %>
以上就是关于“asp字符转二进制流”的问题,朋友们可以点击主页了解更多内容,希望可以够帮助大家!
文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/56512.html<