c# - Custom encoding map multiple characters to one byte -


i trying write custom encoding, allows me use table file map specific characters bytes. unfortunately there characters not inside common unicode sheet (like buttons of video game, end of string identifiers, special ligaturs) wanted include encoding via escapes, example "\du" or "direction up" map arrow kind of character. problem is, if encoder gets character buffer "mid-sentence" there might not complete escape string in buffer, , program throws exception. (this happens when using streams example, create 1-4kb chunks of char data)

is there way of telling c# encoder "reconsider" string, abort here when slidng further take @ characters again because might incomplete?

my code, if of looks follows:

public override int getbytes(char[] chars, int charindex, int charcount, byte[] bytes, int byteindex)     {         if (chars == null) throw new argumentnullexception("chars");         if (charindex > chars.length || charindex < 0) throw new argumentoutofrangeexception("charindex");         if (charindex + charcount > chars.length) throw new argumentoutofrangeexception("charcount");         if (bytes == null) throw new argumentnullexception("bytes");         if (byteindex > bytes.length) throw new argumentoutofrangeexception("byteindex");          string s = new string(chars, charindex, charcount);         int pos = 0;         int num = 0;         while (pos < s.length)         {             int len;             bytes[byteindex + num] = table.encode(s, pos, out len);             pos += len;`          }         return num;     } 

table.encode:

public byte encode(string s, int position, out int length) {     s = regex.replace(s, "\r\n|\r|\n", _linebreak);     if (position > s.length || position < 0) throw new argumentoutofrangeexception("position");     //characters array conversion information, "a -> 07h" or "\du -> 1fh"     (int b = 0; b < characters.length; b++)     {         if (characters[b].matches(s, position))         {             length = characters[b].length;             return (byte)b;         }     }     //is thrown if there unmatched character     throw new exception("unmatched character."); } 


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -