次の方法で共有


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 https://windowssdk.msdn.microsoft.com/en-us/library/tt6z1500.aspx, 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

  • Anonymous
    October 03, 2006
    Hi Shawn, I'll be very happy to see some Own Encoding example here. Regards, Ivan.

  • Anonymous
    October 12, 2006
    Previously I wrote about the Best Way to Make Your Own Encoding , but didn't include an example, so today

  • Anonymous
    May 19, 2007
    Warning: what is being described in this post is documented but is not supported. Please keep this mind

  • Anonymous
    May 20, 2011
    my encoding #@$#%^&&&$#%#&#^^&^%$####@@#$#@%$@#@#~!@~!~@%^**((&^%^$* and 1111111111010000000000010001010101001010101010

  • Anonymous
    September 22, 2012
    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.