什么是摩尔斯电码

摩尔斯电码(又译为摩斯密码,Morse code)是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母数字标点符号。它发明于1837年,发明者有争议,是美国人塞缪尔·莫尔斯或者艾尔菲德·维尔。 摩尔斯电码是一种早期的数字化通信形式,但是它不同于现代只使用零和一两种状态的二进制代码,它的代码包括五种: 点、划、点和划之间的停顿、每个字符之间短的停顿、每个词之间中等的停顿以及句子之间长的停顿。
摩尔斯电码由两种基本信号组成:短促的点信号“·”,读“滴”;保持一定时间的长信号“—”,读“嗒”。间隔时间:滴=1t,嗒=3t,滴嗒间=1t,字符间=3t,单词间=7t。
了解更多
最常用的SOS即为··· --- ···

在C#中的实现

编码解码和发声

public static class MorseCode
    {
        /// <summary>
        /// 摩尔斯密码表
        /// </summary>
        private static volatile string[,] CodeTable =
        {
        {"A",".-"},
        {"B","-..."},
        {"C","-.-."},
        {"D","-.."},
        {"E","."},
        {"E","..-.."},
        {"F","..-."},
        {"G","--."},
        {"H","...."},
        {"I",".."},
        {"J",".---"},
        {"K","-.-"},
        {"L",".-.."},
        {"M","--"},
        {"N","-."},
        {"O","---"},
        {"P",".--."},
        {"Q","--.-"},
        {"R",".-."},
        {"S","..."},
        {"T","-"},
        {"U","..-"},
        {"V","...-"},
        {"W",".--"},
        {"X","-..-"},
        {"Y","-.--"},
        {"Z","--.."},
        {"0","-----"},
        {"1",".----"},
        {"2","..---"},
        {"3","...--"},
        {"4","....-"},
        {"5","....."},
        {"6","-...."},
        {"7","--..."},
        {"8","---.."},
        {"9","----."},
        {".",".-.-.-"},
        {",","--..--"},
        {":","---..."},
        {"?","..--.."},
        {"\'",".----."},
        {"-","-....-"},
        {"/","-..-."},
        {"(","-.--."},
        {")","-.--.-"},
        {"\"",".-..-."},
        {"=","-...-"},
        {"+",".-.-."},
        {"*","-..-"},
        {"@",".--.-."},
        {"{UNDERSTOOD}","...-."},
        {"{ERROR}","........"},
        {"{INVITATION TO TRANSMIT}","-.-"},
        {"{WAIT}",".-..."},
        {"{END OF WORK}","...-.-"},
        {"{STARTING SIGNAL}","-.-.-"},
        {" ","\u2423"}
    };

        /// <summary>
        /// 编码
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>编码后字符串,每个编码以空格分开</returns>
        public static string Enc(string str)
        {
            int i;
            string ret = string.Empty;
            if (str != null && (str = str.ToUpper()).Length > 0)
                foreach (char asc in str)
                    if ((i = Find(asc.ToString(), 0)) > -1)
                        ret += " " + CodeTable[i, 1];
            return ret;
        }

        /// <summary>
        /// 解码
        /// </summary>
        /// <param name="str">需要解码的字符串,每个编码需要空格隔开</param>
        /// <returns>解码后字符串</returns>
        public static string Dec(string str)
        {
            int i;
            string[] splits;
            string ret = string.Empty;
            if (str != null && (splits = str.Split(' ')).Length > 0)
            {
                foreach (string split in splits)
                    if ((i = Find(split, 1)) > -1)
                        ret += CodeTable[i, 0];
                return ret;
            }
            return "{#}";
        }

        /// <summary>
        /// 查表
        /// </summary>
        /// <param name="str">需要查找字符串</param>
        /// <param name="cols">查表方式 0:编码 1:解码</param>
        /// <returns></returns>
        private static int Find(string str, int cols)
        {
            int i = 0, len = CodeTable.Length / 2; // len / rank  
            while (i < len)
            {
                if (CodeTable[i, cols] == str)
                    return i;
                i++;
            };
            return -1;
        }

        /// <summary>
        /// 调用系统dll文件实现蜂鸣器播放
        /// </summary>
        /// <param name="frequency">声音频率</param>
        /// <param name="duration">播放时间</param>
        /// <returns></returns>
        [DllImport("Kernel32.dll")]
        public static extern bool Beep(int frequency, int duration);

        /// <summary>
        /// 使用电脑蜂鸣器播放声音
        /// </summary>
        /// <param name="_str">需要播放的摩尔斯密码</param>
        public static void Play(string _str)
        {
            foreach (char c in _str)
            {
                switch (c)
                {
                    case '-':
                        Beep(3000, 300);        //3000HZ蜂鸣器响,时间300ms
                        break;
                    case '.':
                        Beep(3000, 100);        //3000HZ蜂鸣器响,时间100ms
                        Thread.Sleep(100);      //当前线程睡眠100ms
                        break;
                    case ' ':
                        Thread.Sleep(300);      //当前线程睡眠300ms
                        break;
                }
            }
        }
    }

补充

使用如上方法,不能解码二进制,并且没有明显的分隔符号
那么
Replace来了!
编码时" "转化成"/"

word2= word1.Text.Replace(" ", "/").Substring(1, DT_ToText.Text.Length - 1);//防止把第一个空格换成"/"

解码时二进制转换

word2 =word1.Replace("0", ".").Replace("1", "-").Replace("/"," ");
Last modification:February 4th, 2020 at 04:30 pm
您的每次打赏,都是我更新的不竭动力!