How to Convert a String into Bytes Array in .NET

There are many methods in .NET Framework which require you to pass an array of bytes and if you want to convert a string into bytes array you can use GetBytes method as follows:

string s1 = "Microsoft"; 
byte[] ar = Encoding.ASCII.GetBytes(s1);

You can also convert a byte array into string using GetString method as shown below:

string s2 = Encoding.ASCII.GetString(ar);



About author

User avatar

Waqas Anwar

Founder of, Senior Consultant, Microsoft .NET and Web Technologies