如何实现Baudot编码 [英] How to implement Baudot encoding

查看:159
本文介绍了如何实现Baudot编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施 Baudot字符编码(每个字符代码6位)在.Net。这是一个 Cospas Sarsat设备

I'm trying to implement a Baudot character encoding (a 6 bit per character code) in .Net. It's for a Cospas Sarsat device.

我开始从编码类派生:

public class BaudotEncoding : Encoding {



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

I'm looking for a simple, efficient way to implement a bidirectional character map (the map can be readonly):

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

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

I also need to figure out how to implement the GetBytes method of System.Text.Encoding

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



我无法确定如何实现此方法,

I am having trouble figuring out how to implement this method because the characters don't fit in nice 8 bit sets.

推荐答案

简单的字符串常量可能足以将chars映射到int值,可能比Dictionary快。这种快速投射在一起的代码显示了我在您先前的问题。我不知道你想如何处理数字/字母问题,你想要添加范围检查参数。你还需要测试正确性。但它显示了将char值放在字符串中并使用它在两个方向查找的想法。给定一个int值,它会尽快得到。给定一个字符,反向查找也会非常快。

Simple string constants may be sufficient for the mapping of chars to int values, and possibly faster than Dictionary. This quickly thrown together code shows the idea of what I was describing in your previous question. I don't know how you want to handle the figures/letters issue, and you'd want to add range checking on arguments. You'll also need to test for correctness. But it shows the idea of just putting the char values in a string and using that to look up in both directions. Given an int value, it will be as fast as you can get. Given a char to do the reverse lookup will, I expect, be extremely fast as well.

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,贝尔钟(如果有这样的事情)。我只是用快速小写字母为示范目的。

You will also probably want to modify the simple handling of special characters I define as constants. For example, using char(0) for null, ASCII bell for bell (if there is such a thing). I just threw in quick lowercase letters for demonstration purposes.

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

I used nullable return values to demonstrate the notion of not finding something. But it might be simpler to just return the Undefined constant if a given int value does not map to anything, and -1 if the given char is not in the Baudot character set.

这篇关于如何实现Baudot编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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