Best Way to Make Your Own Encoding

Martin recently asked what the best way to roll his own encoding in .Net 2.0, in particular can you override Encoding/Encoder/Decoder, or should he write his own StreamWriter.

#1 is, of course, to use Unicode :), but apparently Martin doesn't have that option.

The answer is that you can write your own Encoding derived from the Encoding class and use it as any of the built-in Encodings.  They'll be a little slower in some cases due to some shortcuts we take internally, but otherwise you should be able to use them everywhere you use a normal Encoding/Encoder/Decoder object. 

There's an example of using the fallbacks at, which isn't quite the same thing, but might help a little.

[updated 12 Oct 2006]
I've stuck an example of overriding encodings in this post: Example of overriding your own Encoding.

- Shawn

Comments (5)

  1. Ivan Petrov says:

    Hi Shawn,

    I’ll be very happy to see some Own Encoding example here.



  2. Previously I wrote about the Best Way to Make Your Own Encoding , but didn’t include an example, so today

  3. Warning: what is being described in this post is documented but is not supported. Please keep this mind

  4. genius says:

    my encoding #@$#%^&&&$#%#&#^^&*^*%$####@@#$#@%$@#@#~!@~!~@%^***((&^%*^$*

    and 1111111111010000000000010001010101001010101010

  5. Joshua says:

    I made an encoding once that mapped bytes 0-255 to Unicode code points 0-255 (yes I know 0 is not an allowed Unicode code point). The purpose was a converter that would round-trip binary data into string objects. Funny, the 0 Unicode code oint didn't break any core .NET libraries.

Skip to main content