How to work with encoding in .NET?

By | August 23, 2016

Experts working in reputed .Net development company talk about the way to use .net functionalities to perform encoding in .net. Read this story and learn more about encoding.

Today I want to talk about how to use .NET functionalities to perform Encoding in .NET. For that purpose is first necessary to make a general overview around encoding patterns. Encoding is subject that causes a lot of confusion and is very misunderstood by developers.Encoding In .NET

You can imagine encoding patterns as a list of all possible characters belonging to that pattern. For example: the ASCII pattern has only English language characters and some punctuation characters. Since the ASCII was not suitable for some regions (where a language different from English is used), other patterns were created like UTF8, UTF16, etc…

Inside of namespace System. Text we have a call encoding, which centralizes all of work you’ve been doing if using encoding manually.
You can list all encodings available within your computer using the following code:

The class Encoding has a static method called GetEncodings, which returns an array of EncodingInfo type. The same has some properties which could be used for recovery of some information. In the example above we’ve used properties like CodePage, Name and DisplayName.

You may also use other methods provided by .NET framework, which provides several functionalities depending on your case. For example a GetBytes() method retrieves a bytes sequence for the text that we are trying to encode (using an encoder specified). Other is IsAlwaysNormalized() method, which retrieves a value that shows weather actual codification is always normalized.

For each character that you use in your string, there is corresponding byte sequence in the encoding that you choose. You can verify a difference of bytes executing the following code:

When you execute this code you will receive different representation for the letter “ç” depending on the encoding that you are using.

There is very important that you work with a right encoding when you perform Read/Write operations. In the example below is shown how to write a text using UTF7 encoding. And then following how a Read operation of the same text should be performed using right and wrong encoding.

You could understand that if we use wrong encoding than we may face several problems at the time of reading the text.

If you have a possibility to choose which class of encoding to use, go for UTF8Encodign or UnicodeEncodign (Recommendation of Microsoft), otherwise if there is impossible to use that encodings you can choose an encoding that is better fits your needs.

One more subtopic that is important to mention is related to the Encoding fallbacks. In the subject of encoding a fallback is a functionality which enables you to deal with unrecognized sequences of bytes (unrecognized characters). Basically when you try to encode or decode text which is not in the encoding you’ve choose, you must implement a way that will decide how the failed conversion should be handled.

You may have several custom strategies to apply in that situation, but I want to mention one that is the most used one, which is Replacement Fallback Strategy, basically in that situation when you encoding or decoding some text and for some reason no correspondent character is found, your fallback will replace that character with predefined one (For example the ASCII encoding when no corresponding character is found it replaces that character with a “?” mark).

You can find 2 methods to deal with that fallbacks in .NET an EncoderReplacementFallback and a DecoderReplacementFallback. This two methods have a “?” as a predefined letter to be replaced with unrecognized characters, but if you override their behavior you can choose your own symbol to be a default one for unrecognized characters.
We hope that it was useful and will save you much time with text Reading and Writing operations using .NET framework.

Hope the experts of .net development company have made you understand about the encoding functionality use in .net. You can still ask for more info related to encoding functionalities in comments.

Author bio:
Aaron Jacobson is working as content developer in a reputed asp.net Development company. You can share your thoughts regarding this post with her and suggest some topics. She will pick the interesting one and share her thoughts on the same.