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
493 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
ID: 9903203
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
ID: 9903216
0
 
LVL 1

Author Comment

by:Lycaon
ID: 9904323
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 28

Accepted Solution

by:
sybe earned 355 total points
ID: 9904403
- 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
 
LVL 28

Expert Comment

by:sybe
ID: 9904416
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
ID: 9909824
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
ID: 9930593
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
ID: 9930742
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

786 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