Do you like this article? Please, rate it and write review!
Rated:
by Aspin.com users
| |
| | Top messages |
| 22.3.2003 19:18:41 | |
| 4.5.2002 9:16:43 | |
| 12.6.2003 9:14:29 | |
Create and work with binary data in ASP/VBScript | Areas>Languages>VBScript Areas>ASP / ASP.Net>Functions>Conversion Areas>ASP / ASP.Net>Response control | |
|
ASP cannot work with real binary data (Safearray of bytes, VT_UI1 | VT_ARRAY ). But you can use multibyte string functions which enables work with byte array data. Most of the standard string functions (Mid, Left, Len, ...) have its multibyte twins (MidB, LeftB, LenB, ...).
Although the functions are pure commented in ASP/VBS documentation, you can create and convert binary data from BinaryRead and BinaryWrite by the set of functions :
Multibyte functions, BinaryRead and BinaryWrite
Dim i, MyMultiByte
For i=1 To 255
MyMultiByte = MyMultiByte & ChrB(i)
Next
Response.Buffer = True
Response.ContentType = "Application/Crazzy"
Response.BinaryWrite MyMultiByte
|
You can also create convert function from String to binary :
Function StringToMultiByte(S)
Dim i, MultiByte
For i=1 To Len(S)
MultiByte = MultiByte & ChrB(Asc(Mid(S,i,1)))
Next
StringToMultiByte = MultiByte
End Function
|
And use the function :
Response.BinaryWrite StringToBinary("My first binary string.")
|
You can work with binary data from BinaryRead by the same way :
Binary = Request.BinaryRead(Request.ServerVariables("HTTP_Content_Length"))
bString = BinaryToString(Binary)
Response.Write bString
'Of course, you can use Response.BinaryWrite Binary :-)
|
Where BinaryToString is :
Function BinaryToString(Binary)
Dim I, S
For I = 1 To LenB(Binary)
S = S & Chr(AscB(MidB(Binary, I, 1)))
Next
BinaryToString = S
End Function
|
Function BinaryToString accepts binary or multibyte data on input (Binary parameter)
Real binary data
Although VBS cannot work with real binary data, you can use some free object to convert string data to binary.
Stream_StringToBinary fuction lets you convert string to binary data using ADODB.Stream. Stream_StringToBinary returns real binary data - VT_UI1 | VT_ARRAY Variant type.
'Stream_StringToBinary Function
'2003 Antonin Foller, http://www.motobit.com
'Text - string parameter To convert To binary data
'CharSet - charset of the Text - default is "us-ascii"
Function Stream_StringToBinary(Text, CharSet)
Const adTypeText = 2
Const adTypeBinary = 1
'Create Stream object
Dim BinaryStream 'As New Stream
Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To save text/string data.
BinaryStream.Type = adTypeText
'Specify charset For the source text (unicode) data.
If Len(CharSet) > 0 Then
BinaryStream.CharSet = CharSet
Else
BinaryStream.CharSet = "us-ascii"
End If
'Open the stream And write text/string data To the object
BinaryStream.Open
BinaryStream.WriteText Text
'Change stream type To binary
BinaryStream.Position = 0
BinaryStream.Type = adTypeBinary
'Ignore first two bytes - sign of
BinaryStream.Position = 0
'Open the stream And get binary data from the object
Stream_StringToBinary = BinaryStream.Read
End Function
|
To save/retrieve binary data from disk, binary file upload and download in ASP or better work with binary data (use any code page - ANSI, MAC, unicode UTF-8, search binary data, etc.) see Pure and Huge asp file upload. |
|
If you like this page, please include next link on your pages:
<A
Href="http://www.motobit.com/tips/detpg_BinASP/"
Title="Lets you convert/create binary data in
ASP to use the data
for BinaryWrite/BinaryRead."
>Create and work with binary data in ASP/VBScript</A>
|
|