.NET     Console.WriteLine( "All Things .NET" );
.NET Nerd Blog Home
2.15.2005

 
String to Bytes and Back
Just a quick cheat sheet on converting to/from string to byte[] and back. I never remember exactly the syntax, so always spend a few minutes looking it up.
//Convert string to byte
byte[] bytes = System.Text.Encoding.Default.GetBytes("The string");

//Convert to Base64, view/save in config files, etc
string base64 = Convert.ToBase64String( bytes );

//Convert from Base64, back to string
bytes = Convert.FromBase64String( base64 );

//Convert byte to string
string msg = System.Text.Encoding.Default.GetString(bytes);

//Create memory stream based on byte array
MemoryStream ms = new MemoryStream(bytes);
StreamReader sr = new StreamReader(ms);


keywords: string byte convert base64 hex


Comments: Post a Comment

Powered by Blogger