Skip to main content
Added complete namespace
Source Link
ba0708
  • 10.7k
  • 15
  • 71
  • 102

Assuming that you are using UTF-8 encoding:

string convert = "This is the string to be converted";

// From string to byte array
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(convert);

// From byte array to string
string s = System.Text.Encoding.UTF8.GetString(buffer, 0, buffer.Length);
string convert = "This is the string to be converted";

// From string to byte array
byte[] buffer = Encoding.UTF8.GetBytes(convert);

// From byte array to string
string s = Encoding.UTF8.GetString(buffer, 0, buffer.Length);

Assuming that you are using UTF-8 encoding:

string convert = "This is the string to be converted";

// From string to byte array
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(convert);

// From byte array to string
string s = System.Text.Encoding.UTF8.GetString(buffer, 0, buffer.Length);
Updated encoding
Source Link
ba0708
  • 10.7k
  • 15
  • 71
  • 102
string convert = "This is the string to be converted";

// From string to byte array
byte[] buffer = Encoding.ASCIIUTF8.GetBytes(convert);

// From byte array to string
string s = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
string convert = "This is the string to be converted";

// From string to byte array
byte[] buffer = Encoding.ASCII.GetBytes(convert);

// From byte array to string
string s = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
string convert = "This is the string to be converted";

// From string to byte array
byte[] buffer = Encoding.UTF8.GetBytes(convert);

// From byte array to string
string s = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
Source Link
ba0708
  • 10.7k
  • 15
  • 71
  • 102

string convert = "This is the string to be converted";

// From string to byte array
byte[] buffer = Encoding.ASCII.GetBytes(convert);

// From byte array to string
string s = Encoding.UTF8.GetString(buffer, 0, buffer.Length);