473,698 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1100
* William Morgan <wm*****@madiso ncounty.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*****@madiso ncounty.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(B yVal InString As String, ByVal
Bytes As Integer) As Byte()
Dim output As New MemoryStream(In String.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.C hars(i)) 'Get 4 chars and put into 'y's
y1 = AscW(InString.C hars(i + 1))
y2 = AscW(InString.C hars(i + 2))
y3 = AscW(InString.C hars(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.WriteByt e(CByte(x0))
output.WriteByt e(CByte(x1))
output.WriteByt e(CByte(x2))
Next i
Return output.GetBuffe r()
End Function

Which actually makes much more sense! :-)

Hope this helps
Jay
"William Morgan" <wm*****@madiso ncounty.net> wrote in message
news:d1******** *************** *********@4ax.c om...
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(By Val InString As String, ByVal
Bytes As Integer) As Byte()
Dim output As New MemoryStream(In String.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.C hars(i)) 'Get 4 chars and put into 'y's
y1 = AscW(InString.C hars(i + 1))
y2 = AscW(InString.C hars(i + 2))
y3 = AscW(InString.C hars(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(bu ffer, 0, 3)
Next i
Return output.GetBuffe r()
End Function

Hope this helps
Jay

"William Morgan" <wm*****@madiso ncounty.net> wrote in message
news:d1******** *************** *********@4ax.c om...
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.SetLengt h(Bytes)
End If Return output.GetBuffe r()
End Function
Hope this helps
Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:%2******** ********@tk2msf tngp13.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(B yVal InString As String, ByVal
Bytes As Integer) As Byte()
Dim output As New MemoryStream(In String.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.C hars(i)) 'Get 4 chars and put into 'y's y1 = AscW(InString.C hars(i + 1))
y2 = AscW(InString.C hars(i + 2))
y3 = AscW(InString.C hars(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.WriteByt e(CByte(x0))
output.WriteByt e(CByte(x1))
output.WriteByt e(CByte(x2))
Next i
Return output.GetBuffe r()
End Function

Which actually makes much more sense! :-)

Hope this helps
Jay
"William Morgan" <wm*****@madiso ncounty.net> wrote in message
news:d1******** *************** *********@4ax.c om...
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.Ge tBuffer() returns the underlying buffer itself, you need to
use MemoryStream.To Array() to return the byte array created...
Next i If output.Length > Bytes Then
output.SetLengt h(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******** ******@TK2MSFTN GP12.phx.gbl... Doh!

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

If output.Length > Bytes Then
output.SetLengt h(Bytes)
End If
Return output.GetBuffe r()
End Function


Hope this helps
Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:%2******** ********@tk2msf tngp13.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(B yVal InString As String,

ByVal Bytes As Integer) As Byte()
Dim output As New MemoryStream(In String.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.C hars(i)) 'Get 4 chars and put into

'y's
y1 = AscW(InString.C hars(i + 1))
y2 = AscW(InString.C hars(i + 2))
y3 = AscW(InString.C hars(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.WriteByt e(CByte(x0))
output.WriteByt e(CByte(x1))
output.WriteByt e(CByte(x2))
Next i
Return output.GetBuffe r()
End Function

Which actually makes much more sense! :-)

Hope this helps
Jay
"William Morgan" <wm*****@madiso ncounty.net> wrote in message
news:d1******** *************** *********@4ax.c om...
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*****@madiso ncounty.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
3492
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 buffers. Basically, I want to be able to use all the standard read and write functions, but I want them to refer to memory locations, rather than disk files. I do not want to touch the legacy code. Does any one know of a library
4
2119
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 then read it, but i think it's another way to do this in memory, using MemoryStream or other object. No sucess.
5
7845
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 COM control (like Microsoft WebBrowser). For each description there is one HTML file and along description text, it contains links to related information. Clicking related information would for example open new form in application and display some...
7
5843
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 C++/MFC/EXE app is using the traditional WIN32 commands like OpenFileMapping
5
12985
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 InputStream; Stream OutPutStream; DataSet1.WriteXml(InputStream);
6
20904
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 the line: System.IO.MemoryStream MemoryStream = (System.IO.MemoryStream)WebResponse.GetResponseStream(); Here's my code:
7
4214
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 example using MemoryStream. Is it any possibility to get the char* pointer to the data stored in MemoryStream *without* allocation of unmanaged memory and copying entire stream contents to the newly allocated buffer? Regards,
0
1309
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 use this to send the msg BinaryFormatter b = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); b.Serialize(stream, str); stream.Flush(); int dataSize =(int)...
3
2953
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 directly to disk instead it writes to memory and then at the end on either close/ dispose, it will write the data to disk. I am thinking of compressing the data still, I will grow into the size very fast. Is there an alternate solution to this?...
5
6866
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 again in to word object. If some one has some brilliant idea please share. In a nutshell this is what I'm currently doing ByteArray --File Stream --Word Object and this I want to do
0
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.