Difference between Encoding.UTF8.GetBytes and UTF8Encoding.Default.GetBytes

17,754

There is no UTF8Encoding.Default property. When you write this, you're actually returning the base class static property, Encoding.Default, which is not UTF8 (it's the system's default ANSI code-page encoding).

As such, the two will return very different results - since UTF8Encoding.Default is actually Encoding.Default, you will return the same thing as if you use ASCIIEncoding.Default or any of the other System.Text.Encoding subclasses.

The proper way to use UTF8Encoding is with an instance you create, such as:

MemoryStream stream = new MemoryStream((new UTF8Encoding()).GetBytes(xml));

The above should provide the same results as:

MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
Share:
17,754

Related videos on Youtube

user972255
Author by

user972255

Updated on June 04, 2022

Comments

  • user972255
    user972255 almost 2 years

    Can someone please explain me what is the difference bet. Encoding.UTF8.GetBytes and UTF8Encoding.Default.GetBytes? Actually I am trying to convert a XML string into a stream object and what happens now is whenever I use this line:

      MemoryStream stream = new MemoryStream(UTF8Encoding.Default.GetBytes(xml));
    

    it gives me an error "System.Xml.XmlException: Invalid character in the given encoding"

    but when I use this line it works fine:

      **MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml));**
    

    Even though it seems to be UTF8 encoding in both cases how one works and the other does not?

  • Kevin
    Kevin almost 11 years
    Edit "The property way to use UTF8Encoding" to be "The proper"... wouldn't let me edit it since it's only a 2 character change.
  • Alexei Levenkov
    Alexei Levenkov almost 11 years
    +1 to answer for exact question. @user972255 - please avoid string manipulation when creating XML - you likely face problems with encoding mismatch as soon as your XML contains non-ASCII characters. Using XmlDocument, XDocument or XmlWriter methods to save XML to MemoryStream will save you and users of your data some significant pain dealing with invalid XML files.