在使用binaryreader读取它们时,我是否可以使用一些自定义编码或转换一些base16字符(日语SHIFT-JIS中的两个字节字符)?我的意思是,在读取它们时,例如,如果有 0x00ff,它会将其转换为“\et”,在读取和写入时(编辑文件,并使用二进制写入器写入)。
最佳答案
我认为你想实现你自己的编码和解码,所以问题与你如何实现它们有关,这取决于你的算法。默认 Encoding
仅支持流行的编码,例如 Unicode、BigEndianUnicode、UTF-8...
。我建议您不需要任何类型的自定义编码,因为您可以看到 Encoding
只是一个类,其中包含一些方法来执行实际的 encoding
和 decoding
,它在处理众所周知的流行编码 Unicode
方面对您有很大帮助,...但是对于您自己的编码,您必须实现几乎所有核心功能,如下所示:
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
}
希望对你有帮助!
关于c# - 使用自定义编码或添加自定义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17386062/