Need way to read long/int/byte from file via ASP, or convert 1-2-4 byte strings to byte/int/long

Alright, here's my dilema.

I am working on an ASP script that eventually will read assorted bytes, integers, and longs out of a file.  Problem is, of course, ASP can't really read binary files.  Well, it can, but since there are no specific types in ASP, you're stuck reading the binary data into a string.

Now comes the problem.

I open the file, and read 4 bytes of data.  The string now contains four ASCII characters: UªUª

Now, the first 4 bytes in the file are SUPPOSED to be a long number with a vaule of -1437226411.  My question is, now do I get from the UªUª (which is Chr(85) Chr(170) Chr(85) Chr(170) by the way) and convert into a long number?

I have tried making an ActiveX component with functions to read a binary file, but for some reason either IIS or the control itself closes the file right after it opens.  This ActiveX problem may be a separate question though.  If it is, I will reward separate points accordingly.  see http://lycaon.no-ip.com/code/eequestion.asp if you want to take a crack at it.

James
LVL 1
LycaonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sybeCommented:
Using the ADODB.Stream Object you can do a lot with ASP and binary files (especially good to read and write binary files)

There are ways to convert String to Byte() in ASP using an ADODB.RecordSet (and vice versa).

There's a byte-by-byte conversion way in ASP from String to (semi-)byte and vice versa, using AscB and MidB





0
LycaonAuthor Commented:
sybe,

That page on pstruh.cz that shows the MultiByteToString function doesn't work, it just returns the string I passed to it, instead of returning a long value.

I understand using the Stream object pretty much, but it, and the RecordSet method, both return strings, and as mentioned before, the function on that page isn't working.

Here's the code:

<%

Option Explicit

Dim sStr
Dim FSO, File

Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Set File = FSO.OpenTextFile(Server.MapPath(".") & "\Lycaon-S.d2s", 1)

sStr = File.Read(4)

Response.Write MultiByteToBinary(sStr)

File.Close

Function MultiByteToBinary(MultiByte)
  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

%>

The page us displaying UªUª instead of -1437226411 like it's supposed to (I think it's supposed to).

James
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

sybeCommented:
- If you do a "response.write, then anything will be converted to a String before writing. If no such conversion is possible, then an error occurs. So Using Response.Write is NOT a good way to see if conversion from String to Binary has happened.

- you use MultiByteToBinary in stead of MultiByteToString



Here are the conversion functions as I use them, taken (and adapted a bit) from the pstruh site.

Function RSBinaryToString(ByVal btBinary)
    Dim oRS, iLen
    Set oRS = Server.CreateObject("ADODB.Recordset")
    iLen = LenB(btBinary)
    If iLen > 0 Then
        oRS.Fields.Append "mBinary", 201, iLen
        oRS.Open
        oRS.AddNew
        oRS("mBinary").AppendChunk btBinary
        oRS.Update
        RSBinaryToString = oRS("mBinary").Value
        oRS.Close
    Else
        RSBinaryToString = ""
    End If
    Set oRS = Nothing
End Function

Function RSString2Byte(ByVal s)
    Dim iLenString, oRS
    Set oRS = Server.CreateObject("ADODB.Recordset")
    iLenString = LenB(s)
    If iLenString > 0 Then
        oRS.Fields.Append "mBinary", 205, iLenString
        oRS.Open
        oRS.AddNew
        oRS("mBinary").AppendChunk s & ChrB(0)
        oRS.Update
        RSString2Byte = oRS("mBinary").GetChunk(iLenString)
        oRS.Close
    End If
    Set oRS = Nothing
End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sybeCommented:
If you are testing functions with binaries in ASP, then save the result to a (temporary) file and open the file in binary mode to see what it looks like. That's really the best way, although a bit slow....
0
LycaonAuthor Commented:
sybe,

I meant to use MultiByteToBinary cause I wanted to convert to a number, but I suppose since it it ASP, variables are all variant anyhow, so I suppose it'd work if it returned a string with "-1437226411" in it.  Give me a day to test out all this code, and I'll accept your comment as an answer.  Thanks :)

James
0
LycaonAuthor Commented:
sybe,

The RSString2Byte function is still returning UªUª instead of a long number.  Have you tested this out yourself?  I've even stepped through the code with the Microsoft Script Debugger, and I've noticed the function is assuming that s is a unicode string, and says the length of the string is 8 bytes.  It's not 8 bytes, it's 4.  Perhaps this is why it's not working?

James
0
LycaonAuthor Commented:
sybe,

Nevermind about further research.  I just wrote a DLL to convert a 4 byte string to a long, and it works.  Thanks for the effort though, it pointed me in the right direction. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.