.Net Framework Encoding Sample

A blog post I saw recently (A .Net Unicode Puzzle) reminded me of a sample we’d written for the .Net Framework Samples.  “Fallback Encoding Application Sample” demonstrates the Encoding/Decoding and Fallback classes, using the various APIs provided.

Since our inputs were more limited than other programs might be, we stretched to use some of the methods we wouldn’t have ordinarily chosen, but GetBytes/GetChars, Encoders, Decoders and Convert are all shown.

There’s also an example of an Encoder best fit fallback class, which decomposes a string to normalization form NFKD, and then tries each part to see if it can be encoded.  This is similar to solving the puzzle posed by Chris Mullins with the additional aspect of limiting the output to the set of characters supported by the target encoding.

Comments (2)

  1. Shamika says:

    I need to convert utf16 to utf8. Is anyone have code sample for this

  2. Shawn Steele says:

    Strings in .Net are UTF-16, so if you just call Encoding.GetBytes(myString), it’ll give you a byte array for the bytes in your string.

    MSDN also has similar examples of using encodings in the System.Text.Encoding class topics.  (eg: GetBytes).  Look on search.live.com 🙂