Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
502 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 28

Accepted Solution

by:
sybe earned 1065 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

926 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