Today’s post revolves around a very simple equation and the .NET Framework support for the different parts.
bytes + encoding = text
Typically device resources such as files and network connections will work in terms of bytes. To represent bytes, you can use Byte or more typically the array type, Byte. The class you use to read or ‘pull’ bytes from a source is Stream. To write, you’ll also use Stream. Some streams allow both reading and writing, some only one kind of operation – the CanRead and CanWrite properties tell you what’s possible.
To represent text, you can use Char, String and StringBuilder. The class you use to read text from some source is typically TextReader. The class you use to write text is TextWriter. There are no classes in the .NET Framework that allow both reading and writing text through the same object.
So, how do you go from one end to the other? Enter the Encoding class. The .NET Framework has a number of built-in encodings and also supports a much larger set through the operating system. There are lots of functionality available through this class, including conversion, encoding enumeration and support for fallback when something isn’t supported, but the basic operations are these.
Text to bytes: the GetBytes method.
And those are the basics for today. More on working with bytes and text next time!