欢迎光临
我们一直在努力

[VBS]转换二进制数据为字符串常用办法-ASP教程,数据库相关

建站超值云服务器,限时71元/月

至少有三种以上办法,可以把二进制数据(比如您从asp的request.binaryread方法得到的数据)转换为字符串。

第一种:使用vbs的multibyte 方法

实例:

function simplebinarytostring(binary)

simplebinarytostring converts binary data (vt_ui1 | vt_array or multibyte string)

to a string (bstr) using multibyte vbs functions

dim i, s

for i = 1 to lenb(binary)

s = s & chr(ascb(midb(binary, i, 1)))

next

simplebinarytostring = s

end function

这个方法非常简单明了,但是处理大数据流时,比较慢。

建议只用来处理100kb以下的数据。

下面的这个类似的方法,性能稍微好些:

function binarytostring(binary)

antonin foller, http://www.pstruh.cz

optimized version of a simple binarytostring algorithm.

dim cl1, cl2, cl3, pl1, pl2, pl3

dim l

cl1 = 1

cl2 = 1

cl3 = 1

l = lenb(binary)

do while cl1<=l

pl3 = pl3 & chr(ascb(midb(binary,cl1,1)))

cl1 = cl1 + 1

cl3 = cl3 + 1

if cl3>300 then

pl2 = pl2 & pl3

pl3 = ""

cl3 = 1

cl2 = cl2 + 1

if cl2>200 then

pl1 = pl1 & pl2

pl2 = ""

cl2 = 1

end if

end if

loop

binarytostring = pl1 & pl2 & pl3

end function

binarytostring方法比simplebinarytostring方法性能高20倍。建议用来处理2mb以下的数据。

第二种方法:使用adodb.recordset

adodb.recordset 可以让你支持几乎所有variant支持的数据类型,你可以用它在string和

binary之间转换。

function rsbinarytostring(xbinary)

antonin foller, http://www.pstruh.cz

rsbinarytostring converts binary data (vt_ui1 | vt_array or multibyte string)

to a string (bstr) using ado recordset

dim binary

multibyte data must be converted to vt_ui1 | vt_array first.

if vartype(xbinary)=8 then binary = multibytetobinary(xbinary) else binary = xbinary

dim rs, lbinary

const adlongvarchar = 201

set rs = createobject("adodb.recordset")

lbinary = lenb(binary)

if lbinary>0 then

rs.fields.append "mbinary", adlongvarchar, lbinary

rs.open

rs.addnew

rs("mbinary").appendchunk binary

rs.update

rsbinarytostring = rs("mbinary")

else

rsbinarytostring = ""

end if

end function

rsbinarytostring 没有什么限制–除了物理内存之外。这种处理方式是multibyte方式的100倍!你可以用它来处理高达100mb的数据! 这种转换方式,你也可以用来把multibyte strings转换为string。下面这个方法把multibyte strings转换为binary:function multibytetobinary(multibyte)

&copy; 2000 antonin foller, http://www.pstruh.cz

multibytetobinary converts multibyte string to real binary data (vt_ui1 | vt_array)

using recordset

dim rs, lmultibyte, binary

const adlongvarbinary = 205

set rs = createobject("adodb.recordset")

lmultibyte = lenb(multibyte)

if lmultibyte>0 then

rs.fields.append "mbinary", adlongvarbinary, lmultibyte

rs.open

rs.addnew

rs("mbinary").appendchunk multibyte & chrb(0)

rs.update

binary = rs("mbinary").getchunk(lmultibyte)

end if

multibytetobinary = binary

end function

第三种:使用adodb.stream这种方式是比较常用的:stream_binarytostring function

2003 antonin foller, http://www.pstruh.cz

binary – vt_ui1 | vt_array data to convert to a string

charset – charset of the source binary data – default is "us-ascii"

function stream_binarytostring(binary, charset)

const adtypetext = 2

const adtypebinary = 1

create stream object

dim binarystream as new stream

set binarystream = createobject("adodb.stream")

specify stream type – we want to save text/string data.

binarystream.type = adtypebinary

open the stream and write text/string data to the object

binarystream.open

binarystream.write binary

change stream type to binary

binarystream.position = 0

binarystream.type = adtypetext

specify charset for the source text (unicode) data.

if len(charset) > 0 then

binarystream.charset = charset

else

binarystream.charset = "us-ascii"

end if

open the stream and get binary data from the object

stream_binarytostring = binarystream.readtext

end function

要存储、获取二进制数据,从一个本地文件、上传的二进制数据文件或者asp中,可以参考:pure and huge asp file upload with progress.。 tip keywords: binary, byte, array, vt_ui1, vt_array, binarywrite, binaryread, chrb, instrb, leftb, midb, rightb, asp, vbscopyright and permitted use of http://www.pstruh.cz/tips website. the entire contents of pstruh software website consist of copyright material owned by antonin foller, pstruh software.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » [VBS]转换二进制数据为字符串常用办法-ASP教程,数据库相关
分享到: 更多 (0)

相关推荐

  • 暂无文章