I asked this question a few days ago but it seems to have gone cold fairly quickly. What I want to do is pretty simple and I can't believe someone hasn't figured it out.
Solution needs to be JScript classic ASP. I am reading a file from a remote server and I want to process that (binary) file on my server and spit the results back to the client as XML.
Here's a simplified version of what I am trying to do. This code runs, or will if the URL is filled in for your site. This test file is readbin.asp. It reads a file called test.bin, and writes the result to a stream. I used a stream because that makes it easier to read the file and parse the contents. Basically I want to:
while not end of stream
read byte from stream
process byte
here is 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>") ;
%>
My problem is: how do I get ByteValue as a number 0..255? typeof (ByteValue) is "unknown".
Ord?? Byte()?? Asc?? Chr??
You may want to take a look at this piece of code: http://docs.hyperweb.no/source/asplib1.2/util/fileupload.asp
This code is for handling uploaded files. It is really quite similar: - On line 224 the binary request is being read into a stream object. - On line 232 the data is read back as ISO-8859-1 text, which is almost what you want - You can then read each byte of this string by using the getByte() function on line 48. This function uses the lookup table on line 33 to fix ceratin characters that get converted to unicode.
Maybe not quite on the topic, but using VBScript I wrote this:
Key to this solution is to know that the variable 'data' contains an array of bytes. You can handle that by using the function lenb (length of byte array) and midb (to extract one or more bytes).
Run the script as follows:
This will output to standard out the hex code of all the binary file data. Each line of 16 hex codes is prefixed by a decimal + hex counter of the byte number. THe last column displays readable ascii between 33 and 254.
Also great to circumvent that annoying editor that interprets your UTF-8 codes, if you want to see just the exact codes in your ascii files.
I am much more experienced with vbscript than jscript but I will give it a shot since not many takers on this question.
states there are six possible values that typeof returns: "number," "string," "boolean," "object," "function," and "undefined."
http://msdn.microsoft.com/en-us/library/259s7zc1(VS.85).aspx
The ADODB.Stream .Read object and method return a variant data type. I suspect typeof does not like the variant datatype.
http://www.w3schools.com/ado/ado_ref_stream.asp
The posting from this guy seems to explain it a bit further.
http://blogs.msdn.com/jaiprakash/archive/2007/01/09/jscript-supports-safearrays-of-variants-only.aspx
I would try casting the return stream before applying typeof to it.