【问题标题】:How to implement Baudot encoding如何实现Baudot编码
【发布时间】:2014-03-22 04:04:57
【问题描述】:

我正在尝试在 .Net 中实现 Baudot character encoding(每个字符代码 6 位)。这是给Cospas Sarsat device的。

我从Encoding 类开始:

public class BaudotEncoding : Encoding {

我正在寻找一种简单、有效的方式来实现双向字符映射(映射可以是只读的):

Dictionary<char, int> CharacterMap = new Dictionary<char, int> {
    { ' ', 0x100100 },
    { '-', 0x011000 },
    { '/', 0x010111 },
    { '0', 0x001101 },
    { '1', 0x011101 },
    { '2', 0x011001 },
    ...
}

我还需要弄清楚如何实现System.Text.EncodingGetBytes方法

public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) {

我无法弄清楚如何实现此方法,因为字符不适合漂亮的 8 位集合。

【问题讨论】:

    标签: c# .net character-encoding


    【解决方案1】:

    简单的字符串常量可能足以将 chars 映射到 int 值,并且可能比 Dictionary 更快。这个快速拼凑的代码显示了我在your previous question 中描述的想法。我不知道您想如何处理数字/字母问题,并且您想添加对参数的范围检查。您还需要测试正确性。但它显示了将 char 值放在字符串中并使用它在两个方向上查找的想法。给定一个 int 值,它会尽可能快。给定一个 char 来进行反向查找,我预计也会非常快。

    public class Baudot {
        public const char Null = 'n';
        public const char ShiftToFigures = 'f';
        public const char ShiftToLetters = 'l';
        public const char Undefined = 'u';
        public const char Wru = 'w';
        public const char Bell = 'b';
        private const string Letters = "nE\nA SIU\rDRJNFCKTZLWHYPQOBGfMXVu";
        private const string Figures = "n3\n- b87\rw4',!:(5\")2#6019?&u./;l";
    
        public static char? GetFigure(int key) {
            char? c = Figures[key];
            return (c != Undefined) ? c : null;
        }
    
        public static int? GetFigure(char c) {
            int? i = Figures.IndexOf(c);
            return (i >= 0) ? i : null;
        }
    
        public static char? GetLetter(int key) {
            char? c = Letters[key];
            return (c != Undefined) ? c : null;
        }
    
        public static int? GetLetter(char c) {
            int? i = Letters.IndexOf(c);
            return (i >= 0) ? i : null;
        }
    }
    

    您可能还想修改我定义为常量的特殊字符的简单处理。例如,使用 char(0) 表示 null,使用 ASCII bell 表示 bell(如果有这样的事情)。出于演示目的,我只是输入了快速的小写字母。

    我使用可以为空的返回值来演示找不到东西的概念。但是如果给定的 int 值没有映射到任何东西,则返回 Undefined 常量可能更简单,如果给定的 char 不在 Baudot 字符集中,则返回 -1。

    【讨论】:

    • 这是迄今为止最简单的方法。
    猜你喜欢
    • 2017-09-29
    • 2020-02-04
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 2011-09-01
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多