473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

memory stream help please.

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

Nov 20 '05 #1
8 1084
* William Morgan <wm*****@madisoncounty.net> scripsit:
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()
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2
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.

On Wed, 23 Jun 2004 17:02:25 -0500, William Morgan
<wm*****@madisoncounty.net> wrote:
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

Nov 20 '05 #3
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 Morgan" <wm*****@madisoncounty.net> wrote in message
news:d1********************************@4ax.com...
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

Nov 20 '05 #4
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

"William Morgan" <wm*****@madisoncounty.net> wrote in message
news:d1********************************@4ax.com...
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

Nov 20 '05 #5
Doh!

To set the length of the returned byte array include the following:
Next i If output.Length > Bytes Then
output.SetLength(Bytes)
End If Return output.GetBuffer()
End Function
Hope this helps
Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... 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 Morgan" <wm*****@madisoncounty.net> wrote in message
news:d1********************************@4ax.com...
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


Nov 20 '05 #6
Double Doh!! :-)

MemoryStream.GetBuffer() returns the underlying buffer itself, you need to
use MemoryStream.ToArray() to return the byte array created...
Next i If output.Length > Bytes Then
output.SetLength(Bytes)
End If
Return output.ToArray()
End Function
Hope this helps
Jay
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OI**************@TK2MSFTNGP12.phx.gbl... Doh!

To set the length of the returned byte array include the following:
Next i

If output.Length > Bytes Then
output.SetLength(Bytes)
End If
Return output.GetBuffer()
End Function


Hope this helps
Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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 Morgan" <wm*****@madisoncounty.net> wrote in message
news:d1********************************@4ax.com...
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



Nov 20 '05 #7
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
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

Nov 20 '05 #8

Jay i got it working thanks for all your help..

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

On Wed, 23 Jun 2004 17:02:25 -0500, William Morgan
<wm*****@madisoncounty.net> wrote:
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

Nov 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory...
4
by: Fabiano | last post by:
Please, i've created a webservice that receives a XML string that i need to work as a Dataset. I've tryied the ReadXML method from the DataSet. First i write the XML string at the disk and...
5
by: Tomaz Koritnik | last post by:
Hi I have many short HTML files stored in a binary stream storage to display descriptions for various items in application. HTML would be display inside application using some .NET control or...
7
by: ATS | last post by:
HOWTO Make a C# UserContol Memory Map to a C++/MFC/EXE Please help, I have a UserControl that I want to have "talk" to a C++/MFC/EXE program that is already running via a memory map. The...
5
by: ad | last post by:
I used use SharpZipLib to compress files in disk. But now I want to compress stream into another stream in memory(the stream not associated with disk file) My pseudo is: Stream...
6
by: rh1200la | last post by:
Hey All. I'm trying to download an image from a URL and display it in my page. I'm getting an error converting the Stream to Memory Stream. I'm getting a Specified cast is not valid error for...
7
by: Maciej Oszutowski | last post by:
Hi, I'm going to port my PE manipulation library (written in C) to managed c++ class library. I would like to have opportunity to read content not only from files, but also from memory, for...
0
by: phreak008 | last post by:
I'm using SendMessage(...) to send a message to all other process that might run. It works well. My problem is when I try to pass data using shared memory. Here is my code. In the 1st process, I...
3
by: CSharper | last post by:
I have a resource file that i open to write.After writing almost 300MB I call the close method and It failed with out of memory. One thing I learned that, in C#, resource writers doesn't write data...
5
by: Nitin Mahajan | last post by:
Guys Is there a way in C# to create a word object directly from a memory stream without passing that to hard disk (file stream). I think it doesn't makes sense to create a file just to read it...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.