Web Design that taps into the haromny and vision of your dreams.

C# Convert String to Byte Array and Byte Array to String

Converting between Strings and Byte Array

By on in Coding

44 words, estimated reading time 1 minutes.

These two methods allow conversion between a C# byte array and string, and vice versa. Handy in dealing with COM objects and some .Net Providers (IBM CWBX, EventLog and so on).

String to byte array

public static byte[] StrToByteArray(string str)
{
    System.Text.ASCIIEncoding  encoding = new System.Text.ASCIIEncoding();
    return encoding.GetBytes(str);
}

Byte array to string

public static string ByteArrayToStr(byte[] byteArray)
{
  System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  return encoding.GetString(byteArray);
}

Last updated on: Thursday 22nd June 2017

 

Comments

Have a question or suggestion? Please leave a comment to start the discussion.

 

Leave a Reply

Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Your email address will not be published.