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
495 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP CDO - Limiting Connections 3 65
Insert Button on a table 16 45
Date on a table 16 33
Want an individual results display div 8 39
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
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/…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

680 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