Converting string to byte array in C#


I'm converting something from VB into C#. Having a problem with the syntax of this statement:

if ((searchResult.Properties["user"].Count > 0))
    profile.User = System.Text.Encoding.UTF8.GetString(searchResult.Properties["user"][0]);

I then see the following errors:

Argument 1: cannot convert from 'object' to 'byte[]'

The best overloaded method match for 'System.Text.Encoding.GetString(byte[])' has some invalid arguments

I tried to fix the code based on this post, but still no success

string User = Encoding.UTF8.GetString("user", 0);

Any suggestions?

12/6/2019 4:43:57 PM

First of all, add the System.Text namespace

using System.Text;

Then use this code

string input = "some text"; 
byte[] array = Encoding.ASCII.GetBytes(input);

Hope to fix it!


Also you can use an Extension Method to add a method to the string type as below:

static class Helper
   public static byte[] ToByteArray(this string str)
      return System.Text.Encoding.ASCII.GetBytes(str);

And use it like below:

string foo = "bla bla";
byte[] result = foo.ToByteArray();

var result = System.Text.Encoding.Unicode.GetBytes(text);

static byte[] GetBytes(string str)
     byte[] bytes = new byte[str.Length * sizeof(char)];
     System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
     return bytes;

static string GetString(byte[] bytes)
     char[] chars = new char[bytes.Length / sizeof(char)];
     System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
     return new string(chars);

Why Encoding.Default should not be used...

@Randall's answer uses Encoding.Default, however Microsoft raises a warning against it:

Different computers can use different encodings as the default, and the default encoding can change on a single computer. If you use the Default encoding to encode and decode data streamed between computers or retrieved at different times on the same computer, it may translate that data incorrectly. In addition, the encoding returned by the Default property uses best-fit fallback to map unsupported characters to characters supported by the code page. For these reasons, using the default encoding is not recommended. To ensure that encoded bytes are decoded properly, you should use a Unicode encoding, such as UTF8Encoding or UnicodeEncoding. You could also use a higher-level protocol to ensure that the same format is used for encoding and decoding.

To check what the default encoding is, use Encoding.Default.WindowsCodePage (1250 in my case - and sadly, there is no predefined class of CP1250 encoding, but the object could be retrieved as Encoding.GetEncoding(1250)).

Encoding.ASCII is 7bit, so it doesn't work either, in my case:

byte[] pass = Encoding.ASCII.GetBytes("šarže");
Console.WriteLine(Encoding.ASCII.GetString(pass)); // ?ar?e

...and why UTF-8 encoding should be used instead...

Default encoding is misleading: .NET uses UTF-8 everywhere as the real default (8bit encodings became obsolete by the end of 20. century, check i.e. Console.OutputEncoding.EncodingName*) so every constant you define in the code is UTF-8 encoded by default - so this one should be used unless the data source is in different encoding.

chcp from windows console (cmd) returns 852 in my case, but .NET console silently changes it to UTF-8 and pretends it is the default. However localized system commands (like ping) have the system default codepage hardcoded and it is not UTF-8.

Following Microsoft's recommendation:

var utf8 = new UTF8Encoding();
byte[] pass = utf8.GetBytes("šarže");
Console.WriteLine(utf8.GetString(pass)); // šarže

Encoding.UTF8 recommended by others is an instance uf UTF-8 encoding and can be also used directly or as

var utf8 = Encoding.UTF8 as UTF8Encoding;

...but it is not used always

The encoding for byte arrays should "just work" in Unicode in Western countries, but as soon as you move your program to some less-supported regions (like here in Eastern Europe), it is a real mess: in Czech Republic Windows defaults use (in 2020!) MS non-standard 852 (aka Latin-2) for console, 1250 as Windows OEM, UTF-8 (65001) as .NET (and others) new default and we should keep in mind that some western EU 8bit data is still in 1252, while the old 8bit western standard for Eastern Europe was ISO-8859-2 (aka Latin-2, but NOT the same Latin-2 as 852). Using ASCII means text full of tofu and '?' here. So until the half of 21st century, please set UTF-8 explicitly.