使用自定义编码或添加自定义字符 [英] use a custom encoding or add custom characters

查看:145
本文介绍了使用自定义编码或添加自定义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么可以使用一些自定义编码或转换一些base16字符(两个字节的字符,如日本SHIFT-JIS)当使用二进制读取器阅读它们?我的意思是,当读取它们时,如果有一个0x00ff,它会将其转换为\et,当读取和写入时(编辑文件,并使用binarywriter编写)。

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

推荐答案

我想要实现自己的编码和解码,所以问题与实现它们有关,这取决于你的算法。默认的 Encoding 仅支持流行的编码,如 Unicode,BigEndianUnicode,UTF-8,... 。我建议您不需要任何类型的自定义编码,因为您可以看到编码只是一个类,有一些方法来执行实际的编码解码,它可以帮助您很多处理知名的流行编码 Unicode ,...但是对于你自己的编码,你必须实现几乎所有这些核心的功能:

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
}

希望它有帮助!

这篇关于使用自定义编码或添加自定义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆