ASP Binary data transformation

F

Ferran Foz

Hello,

I'm using ADODB.Stream to open a binary file on the server and write
it down to the browser using Response.BinaryWrite. It's working fine,
but i need to make some changes to the binary data before it is send
to the browser.

I'm trying to use REPLACE, but it's not finding a string that i know
it's in the binary file. Using InstrB i've found that the search
inside the binary data is being done in a Unicode format, but i don't
know how to make an ascii search&replace operation on binary data.

I'd greatly appreciate any help, cause i'm blocked.

thanks,
ferran
 
M

Mark Reichard

Ferran,

Just to make sure I'm clear --- you need an ASCII file on the client,
and is the server binary also ASCII?

I think there are a couple of ways to go. If it is a text file, you
could convert to a Unicode string on the server, do your string
manipulation and then convert back. You could also save as a text
file on the server and provide a hyperlink rather than doing a binary
write.

If you really need to do a binary write, then I think you need to just
do string manipulation by using InstrB. One approach would be to get
the index of the content to replace, build a string byte by byte up to
it, add the new content and then build a string byte by byte from the
remaining content.

In VB you can do something like this:

myString = ""
For intCounter = 1 To intIndexOfReplacedContent
myString = myString & Chr(AscB(MidB(strWebFileText, intCounter,
1)))
Next

For intCounter = 1 To Len(strWebFileText)
myString = myString & Chr(AscB(MidB(strWebFileText, intCounter,
1)))
Next

For intCounter = 1 To Len(strWebFileText) -
intIndexAfterReplacedContent
myString = myString & Chr(AscB(MidB(strWebFileText, intCounter4,
1)))
Next

Then you need to call a function to convert the resulting unicode
string to ASCII. Michael Kaplan posted code to do this --- he has a

WtoA functing in a module called inCodePage. I've re-posted below,
but again this is his code, not mine.

'--------------------------------
' WToA
'
' UNICODE to ANSI conversion, via a given codepage
'--------------------------------
Public Function WToA(ByVal st As String, Optional ByVal cpg As Long =
-1, Optional lFlags As Long = 0) As String
Dim stBuffer As String
Dim cwch As Long
Dim pwz As Long
Dim pwzBuffer As Long
Dim lpUsedDefaultChar As Long

If cpg = -1 Then cpg = GetACP()
pwz = StrPtr(st)
cwch = WideCharToMultiByte(cpg, lFlags, pwz, -1, 0&, 0&, ByVal 0&,
ByVal 0&)
stBuffer = String$(cwch + 1, vbNullChar)
pwzBuffer = StrPtr(stBuffer)
cwch = WideCharToMultiByte(cpg, lFlags, pwz, -1, pwzBuffer,
Len(stBuffer), ByVal 0&, ByVal 0&)
WToA = Left$(stBuffer, cwch - 1)
End Function
---------------------------------------------------------

I haven't tested this, but something like this should work.

Hope it helps,
Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,113
Messages
2,570,688
Members
47,269
Latest member
VitoYwo03

Latest Threads

Top