Convert String to System.IO.Stream [duplicate] - c#

This question already has answers here:
How do I generate a stream from a string?
(12 answers)
Closed 9 years ago.
I need to convert a String to System.IO.Stream type to pass to another method.
I tried this unsuccessfully.
Stream stream = new StringReader(contents);

Try this:
// convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(contents);
//byte[] byteArray = Encoding.ASCII.GetBytes(contents);
MemoryStream stream = new MemoryStream(byteArray);
and
// convert stream to string
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();

To convert a string to a stream you need to decide which encoding the bytes in the stream should have to represent that string - for example you can:
MemoryStream mStrm= new MemoryStream( Encoding.UTF8.GetBytes( contents ) );
MSDN references:
http://msdn.microsoft.com/en-us/library/ds4kkd55%28v=VS.100%29.aspx
http://msdn.microsoft.com/en-us/library/e55f3s5k.aspx

System.IO.MemoryStream mStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes( contents));

string str = "asasdkopaksdpoadks";
byte[] data = Encoding.ASCII.GetBytes(str);
MemoryStream stm = new MemoryStream(data, 0, data.Length);

this is old but for help :
you can also use the stringReader stream
string str = "asasdkopaksdpoadks";
StringReader TheStream = new StringReader( str );

Related

what wrong with my binary compression with C#

I try to compress some binary string with deflate32 but something is going wrong.
string myString = "101111110101010111111111111";
// convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(myString);
MemoryStream stream = new MemoryStream(byteArray);
var compressed = new DeflateStream(stream, CompressionLevel.Fastest);
Console.WriteLine(compressed); // Can't see any compressed string
Where is my mistake?

How to convert UTF16LE to string C#?

I have a file with text in UTF16LE like so
feff 0074 0065 0073 0074
which is basically
test
What I am trying to is to read that file:
using (FileStream stream = File.Open(fileName, FileMode.Open))
{
byte[] b = new byte[stream.Length];
Encoding u16LE = Encoding.Unicode;
arrRaw.Add(u16LE.GetString(b));
}
After that im starting reading those string array in another function:
[![enter image description here][2]][2]
And so the output of that is just hieroglyphs. I don't know what I'm doing wrong. Moreover I was trying to start reading from the third byte in that array, but the result was the same
public static string Utf16ToUtf8Initial(string utf16String)
{
string utf8String = String.Empty;
byte[] utf16Bytes = Encoding.Unicode.GetBytes(utf16String);
byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, utf16Bytes);
utf8String = Encoding.UTF8.GetString(utf8Bytes);
return utf8String;
}

i am converting string to bytes using binary reader, how to reverse the bytes back to same string [duplicate]

This question already has answers here:
How to convert UTF-8 byte[] to string
(16 answers)
Closed 6 years ago.
how to convert bytes to string, i am using below code to convert string to bytes. i want code to convert that bytes to string.
public byte[] FileToByteArray(string fileName)
{
byte[] buff = null;
FileStream fs = new FileStream(fileName,
FileMode.Open,
FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(fileName).Length;
//buff = br.ReadBytes((int)numBytes);
buff = br.ReadBytes(16);
return buff;
}
string result = System.Text.Encoding.UTF8.GetString(byteArray);
also a quicker way to convert a string to byte array
byte[] MyByteArray = str.Select(s => Byte.Parse(s)).ToArray();

Serializing a memorystream object to string

Right now I'm using XmlTextWriter to convert a MemoryStream object into string. But I wan't to know whether there is a faster method to serialize a memorystream to string.
I follow the code given here for serialization - http://www.eggheadcafe.com/articles/system.xml.xmlserialization.asp
Edited
Stream to String
ms.Position = 0;
using (StreamReader sr = new StreamReader(ms))
{
string content = sr.ReadToEnd();
SaveInDB(ms);
}
String to Stream
string content = GetFromContentDB();
byte[] byteArray = Encoding.ASCII.GetBytes(content);
MemoryStream ms = new MemoryStream(byteArray);
byte[] outBuf = ms.GetBuffer(); //error here
using(MemoryStream stream = new MemoryStream()) {
stream.Position = 0;
var sr = new StreamReader(stream);
string myStr = sr.ReadToEnd();
}
You cant use GetBuffer when you use MemoryStream(byte[]) constructor.
MSDN quote:
This constructor does not expose the
underlying stream. GetBuffer throws
UnauthorizedAccessException.
You must use this constructor and set publiclyVisible = true in order to use GetBuffer
In VB.net i used this
Dim TempText = System.Text.Encoding.UTF8.GetString(TempMemoryStream.ToArray())
in C# may apply

Quick way to get the contents of a MemoryStream as an ASCII string

I have a JSON string in a MemoryStream. I am using the following code to get it out as an ASCII string:
MemoryStream memstream = new MemoryStream();
/* Write a JSON string to memstream here */
byte[] jsonBytes = new byte[memstream.Length];
memstream.Read(jsonBytes, 0, (int)memstream.Length);
string jsonString = Encoding.ASCII.GetString(jsonBytes);
What is a shorter/shortest way to do this?
You could use the ToArray method:
using (var stream = new MemoryStream())
{
/* Write a JSON string to stream here */
string jsonString = System.Text.Encoding.ASCII.GetString(stream.ToArray());
}
new StreamReader(memstream, Encoding.ASCII).ReadToEnd()

Categories