Solved

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

Posted on 2003-12-08
8
492 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:Lycaon
  • 4
  • 4
8 Comments
 
LVL 28

Expert Comment

by:sybe
Comment Utility
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
 
LVL 28

Expert Comment

by:sybe
Comment Utility
0
 
LVL 1

Author Comment

by:Lycaon
Comment Utility
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
 
LVL 28

Accepted Solution

by:
sybe earned 355 total points
Comment Utility
- 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

Expert Comment

by:sybe
Comment Utility
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
 
LVL 1

Author Comment

by:Lycaon
Comment Utility
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
 
LVL 1

Author Comment

by:Lycaon
Comment Utility
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
 
LVL 1

Author Comment

by:Lycaon
Comment Utility
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now