memory stream help please.

  • Thread starter Thread starter William Morgan
  • Start date Start date
W

William Morgan

is there a way to take a string that has binary data in it and put it
in the memory stream?

everything i try will not work. it wants it in bytes. and i don't see
a way to convert it over..



thanks
 
* William Morgan said:
is there a way to take a string that has binary data in it and put it
in the memory stream?

everything i try will not work. it wants it in bytes. and i don't see
a way to convert it over..

Untested:

\\\
Dim ms As New MemoryStream(...)
Dim sw As New StreamWriter(ms)
sw.WriteLine(...)
sw.Close()
///
 
not really what i had in mind.. but thanks

what i need to do is take a string that has been uudecoded and put it
in the memory stream until all data has been received.

the only problem is that i don't see a way to convert the string in to
bytes. everything i have tryed gives errors
is there a way to do this? i can write the string to a binary file and
then read the data back out with the binary read. but i don't wont to
write it and read it back out.
 
William,
Strings don't really have binary data in them, at least they should not
really contain binary data. Strings have Unicode characters in them.

If this is related to your previous question, I would alter the DecodeString
function to return a Byte Array instead of a String.

Something like (untested, syntax checked only).

Private Shared Function DecodeString3(ByVal InString As String, ByVal
Bytes As Integer) As Byte()
Dim output As New MemoryStream(InString.Length * 3 \ 4)
Dim x0, x1, x2 As Integer 'These are the chars that will be spit out
Dim y0, y1, y2, y3 As Integer 'These are what we got in

For i As Integer = 0 To Len(InString) - 1 Step 4
y0 = AscW(InString.Chars(i)) 'Get 4 chars and put into 'y's
y1 = AscW(InString.Chars(i + 1))
y2 = AscW(InString.Chars(i + 2))
y3 = AscW(InString.Chars(i + 3))

If (y0 = 96) Then y0 = 32 'If char is 96 then set to 2
If (y1 = 96) Then y1 = 32
If (y2 = 96) Then y2 = 32
If (y3 = 96) Then y3 = 32

x0 = ((y0 - 32) << 2) + ((y1 - 32) >> 4) 'Calculate the 3 chars

x1 = ((y1 Mod 16) << 4) + ((y2 - 32) >> 2)

x2 = ((y2 Mod 4) << 6) + (y3 - 32)

output.WriteByte(CByte(x0))
output.WriteByte(CByte(x1))
output.WriteByte(CByte(x2))
Next i
Return output.GetBuffer()
End Function

Which actually makes much more sense! :-)

Hope this helps
Jay
 
William,
A slightly shorter version...

Something like (untested, syntax checked only).

Private Shared Function DecodeString(ByVal InString As String, ByVal
Bytes As Integer) As Byte()
Dim output As New MemoryStream(InString.Length * 3 \ 4)
Dim y0, y1, y2, y3 As Integer 'These are what we got in
Dim buffer(2) As Byte

For i As Integer = 0 To Len(InString) - 1 Step 4
y0 = AscW(InString.Chars(i)) 'Get 4 chars and put into 'y's
y1 = AscW(InString.Chars(i + 1))
y2 = AscW(InString.Chars(i + 2))
y3 = AscW(InString.Chars(i + 3))

If (y0 = 96) Then y0 = 32 'If char is 96 then set to 2
If (y1 = 96) Then y1 = 32
If (y2 = 96) Then y2 = 32
If (y3 = 96) Then y3 = 32

buffer(0) = CByte(((y0 - 32) << 2) + ((y1 - 32) >> 4))
'Calculate the 3 chars

buffer(1) = CByte(((y1 Mod 16) << 4) + ((y2 - 32) >> 2))

buffer(2) = CByte(((y2 Mod 4) << 6) + (y3 - 32))

output.Write(buffer, 0, 3)
Next i
Return output.GetBuffer()
End Function

Hope this helps
Jay
 
Double Doh!! :-)

MemoryStream.GetBuffer() returns the underlying buffer itself, you need to
use MemoryStream.ToArray() to return the byte array created...
If output.Length > Bytes Then
output.SetLength(Bytes)
End If

Hope this helps
Jay
 
Hi William,

Are you sure you need the MemoryStream, I was thinking that as well in the
beginning until I know the strenght of the StringReader/StringWriter, which
gives in some occations a faster and easier result?

The MemoryStream is very sufficient for Byte arrayss

I hope this gives you an easier solution?

Cor

is there a way to take a string that has binary data in it and put it
 
Jay i got it working thanks for all your help..

this way is a whole lot faster..
thanks again.
 
Back
Top