views:

320

answers:

2

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??

+1  A: 

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.

thomask
Thanks for that. I had already gone down this path sort of. I had determined that the character encoding was munging the bytes and found that using a charset of UTF-16 caused the bytes to be packed as character pairs but otherwise unharmed - I just then read them low byte, then high byte and got the original binary data. Messy though, and you have to handle odd-numbers of bytes specially.Why isn't there a charset called "UTF-8-bit-binary-and-don't-do-any-damned-translation-thanks-I-just-want-the-bytes-unchanged".Is there documentation of the mapping for the various encoding schemes?
I know I've seen this table somewhere more official looking, but here it goes:http://www.alanwood.net/demos/charsetdiffs.html
thomask
Thomask: ISO-8859 and the translation table works fine. Many thanks.
A: 

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.

RandyMorris
so you would replace var ByteValue = BinaryInputStream.read (1) ;by var ByteValue = SomeCastingOperator (BinaryInputStream.read (1)) ;? Any suggestions for how I should cast it?
In addition to the six values of core JScript there is also "date" and "unknown". The value "date" is usually seen with ADODB RecordSet objects with DateTime database columns, while the value "unknown" represents other objects such as "byte arrays", such as the value returned by Stream.Read().
thomask