C# code snipped for converting Stream to String

Posted by craig Sunday, April 26, 2009 1:28:19 PM

 

converting string to MemoryStream I frequently need to perform string manipulations, this code snippet can be useful! Question: How do I convert a string to a Memory C#? Answer:2 step process, 1st string=>byte[] then use MemoryStream // Helper function public static byte[] StrToByteArray(string str) { System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding(); return encoding.GetBytes(str); } public static void Main(){ byte[] bytes=StrToByteArray("testString"); using (MemoryStream ms = new MemoryStream(bytes)) {// do something useful with our stream ms. } }
Copyright 2009 AnyTime
Comments are closed on this post.