在经典的asp / JScript中逐字节访问二进制响应

时间:2010-05-05 09:57:09

标签: asp-classic binary stream

我几天前问了这个问题,但似乎很快就冷了。我想做的事情非常简单,我无法相信有人没有想到它。

解决方案需要是JScript经典ASP。我正在从远程服务器读取文件,我想在我的服务器上处理该(二进制)文件,并将结果作为XML吐回客户端。

这是我想要做的简化版本。如果为您的网站填写了网址,则会运行此代码。此测试文件为readbin.asp。它读取名为test.bin的文件,并将结果写入流。我使用了一个流,因为这样可以更容易地读取文件并解析内容。基本上我想:

while not end of stream
    read byte from stream
    process byte

这里是readbin.asp:

<%@ LANGUAGE = JScript %>
<%
var url = "http:// (... your URL to the file test.bin goes here...) " ; 
var xmlhttp = Server.CreateObject ("MSXML2.ServerXMLHTTP") ;
xmlhttp.open ("GET", url, false) ; 
xmlhttp.send () ; 

var BinaryInputStream = Server.CreateObject ("ADODB.Stream") ;
BinaryInputStream.Type = 1 ; // binary
BinaryInputStream.Open ;
BinaryInputStream.Write (xmlhttp.responseBody) ;
BinaryInputStream.Position = 0 ;

Response.Write ("BinaryInputStream.size = " + BinaryInputStream.size + "<br>") ;
Response.Write ("BinaryInputStream = " + BinaryInputStream + "<br>") ;

var ByteValue = BinaryInputStream.read (1) ;
Response.Write ("ByteValue = " + ByteValue + "<br>") ;
Response.Write ("typeof (ByteValue) = " + typeof (ByteValue) + "<br>") ;
%>

我的问题是:如何将ByteValue作为数字0..255? typeof(ByteValue)是“未知”。

奥德??字节()?? ASC? CHR 13

3 个答案:

答案 0 :(得分:0)

您可能想看一下这段代码: http://docs.hyperweb.no/source/asplib1.2/util/fileupload.asp

此代码用于处理上传的文件。它非常相似: - 在第224行,二进制请求被读入流对象。 - 在第232行,数据以ISO-8859-1文本形式回读,这几乎就是您想要的 - 然后,您可以使用第48行的getByte()函数读取此字符串的每个字节。   此函数使用第33行的查找表来修复转换为unicode的ceratin字符。

答案 1 :(得分:0)

我对vbscript比jscript更有经验,但是我会给它一个机会,因为在这个问题上没有多少人接受它。

state返回有六个可能的值:“number”,“string”,“boolean”,“object”,“function”和“undefined”。
http://msdn.microsoft.com/en-us/library/259s7zc1(VS.85).aspx

ADODB.Stream .Read对象和方法返回变量数据类型。我怀疑typeof不喜欢变体数据类型 http://www.w3schools.com/ado/ado_ref_stream.asp

这个家伙的帖子似乎更进一步解释了这一点 http://blogs.msdn.com/jaiprakash/archive/2007/01/09/jscript-supports-safearrays-of-variants-only.aspx

我会在将typeof应用到它之前尝试转换返回流。

答案 2 :(得分:0)

也许不是关于这个话题,但是使用VBScript我写了这个:

option explicit
dim fso, wshSHell, objShellApp, args, stdin,stdout

set fso         = CreateObject("Scripting.FileSystemObject")
Set wshShell    = CreateObject("WScript.Shell")
set objShellApp = CreateObject("Shell.Application")
Set args = Wscript.Arguments
set stdin = wscript.stdin
set stdout = wscript.stdout

dim filename, txtFile

filename = args(0)

Const adTypeBinary = 1

'Create Stream object
Dim BinaryStream, data
Set BinaryStream = CreateObject("ADODB.Stream")

'Specify stream type - we want To get binary data.
BinaryStream.Type = adTypeBinary

'Open the stream
BinaryStream.Open

'Load the file data from disk To stream object
BinaryStream.LoadFromFile filename

'Open the stream And get binary data from the object
data = BinaryStream.Read
BinaryStream.close

dim i, item, strLine, hexLine
hexLine = ""
strLine = ""

stdout.writeline "Decimal |Hex     |Data                                             | ASCII 33-254"
for i = 0 to lenb(data)-1
  item = ascb(midb(data,i+1,1))
  if ((i MOD 16) = 0) and (i<>0) then
    stdout.writeLine right("00000000" & i,8) & "|" & right("00000000" & hex(i),8) & "|" &  hexLine & " | " & strLine
    hexLine = ""
    strLine = ""
  end if
  hexLine = hexLine & right("0" & hex(item),2) & " "
  if (item <= 32) or (item > 254) then 
    strLine=strLine + "."
  else
    strLine = strLine & chr(item)
  end if
next

此解决方案的关键是知道变量'data'包含一个字节数组。您可以使用函数lenb(字节数组的长度)和midb(提取一个或多个字节)来处理它。

运行脚本如下:

cscript dumphex.vbs my_binary_file.bin > my_binary_file.hex.txt

这将输出标准输出所有二进制文件数据的十六进制代码。每行16个十六进制代码以字节数的十进制+十六进制计数器为前缀。最后一列显示33和254之间的可读ascii。

如果您只想查看ascii文件中的确切代码,那么绕过那些解释您的UTF-8代码的烦人编辑器也很棒。

相关问题