2

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).

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Omarrrio
  • 1,075
  • 1
  • 16
  • 34
  • I don't think you need a custom `Encoding` here, just use some kinds of method to convert between the bytes and the characters yourself. – King King Jun 30 '13 at 00:55
  • i've been looking on how to do it, inside of the reading process, but couldn't find anything :/ – Omarrrio Jun 30 '13 at 00:57

1 Answers1

4

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!

King King
  • 61,710
  • 16
  • 105
  • 130
  • thanks, so the 'public override string GetString(byte[] bytes)' is for the custom encoding, where my code would be to replace the bytes with the appropriate string, right ? is so, why would i need an 'override byte[]' ? – Omarrrio Jun 30 '13 at 15:20
  • @Omarrrio `GetString` is used to decode (convert from bytes to string) while `GetBytes` is used to encode (convert from string to bytes). – King King Jun 30 '13 at 16:13