use a custom encoding or add custom characters

2020-04-16 05:51发布

Is there anyway i can use some custom encoding or convert some base16 characters (two bytes chars as in japanese SHIFT-JIS) when reading them using binaryreader ? i mean, while reading them, like, if there is a 0x00ff it converts it to "\et", when reading and when writing as well (editing the file, and writing using binarywriter).

1条回答
再贱就再见
2楼-- · 2020-04-16 06:22

I think you want to implement your own Encoding and Decoding, so the problem is related to how you implement them, that depends on your algorithm. The default Encoding supports only popular encodings such as Unicode, BigEndianUnicode, UTF-8, .... I suggested that you don't need any kind of custom Encoding, as you can see Encoding is just a class with some methods to perform the actual encoding and decoding, it helps you a lot in handling with the well-known, popular encodings Unicode, ... but for your own Encoding, you must implement almost all the core functions like this:

public class CustomEncoding : Encoding
{
    //NOTE: There are some abstract members requiring you to implement or declare in this derived class.
    public override byte[] GetBytes(string s)
    {
        //Your code goes here            
    }
    public override string GetString(byte[] bytes)
    {
        //Your code goes here
    }
    //And many other virtual (overridable) methods which you can override to implement your custom Encoding fully
}

Hope it helps!

查看更多
登录 后发表回答