zoukankan      html  css  js  c++  java
  • 摩尔斯电码(Morse Code)Csharp实现

    摩尔斯电码,在早期的"生产斗争"生活中,扮演了很重要的角色,作为一种信息编码标准,摩尔斯电码拥有其他编码方案无法超越的长久生命。摩尔斯电码在海事通讯中被作为国际标准一直使用到1999年。不过随着科技的发展,最终还是倒在了历史的车轮下(PS:等等,为何这么熟悉??观众:好一片根正苗红的*文啊,啊啊啊啊),1997年,当法国海军停止使用摩尔斯电码时,发送的最后一条消息是:“所有人注意,这是我们在永远沉寂之前最后的一声呐喊!”。

    任何关于Morse code可拓展的咨询,请移步维基百科:摩尔斯电码

    说明:本文中Csharp实现Morse code如无特殊说明,仅实现了字母长码版数字,不包括标点符号非英语字符特殊字符。(ps:实际上也可以通过下文所实现的方式去实现,不过是不是有点"呆",谁有更好的办法??!!)

    在做事之前,不妨感受一下滴滴答答的声音

    参考国际摩尔斯电码数字字母对应表:

    Morse code集合:  

          /// <summary>
          /// 组织mores集合
          ///我使用了键值对,Dictionary, HashTable都是可以的
          /// </summary>
          Dictionary<char, String> morseCode = new Dictionary<char, String>()
                {
                    {'a' , ".-"},{'b' , "-..."},{'c' , "-.-."}, //alpha
                    {'d' , "-.."},{'e' , "."},{'f' , "..-."},
                    {'g' , "--."},{'h' , "...."},{'i' , ".."},
                    {'j' , ".---"},{'k' , "-.-"},{'l' , ".-.."},
                    {'m' , "--"},{'n' , "-."},{'o' , "---"},
                    {'p' , ".--."},{'q' , "--.-"},{'r' , ".-."},
                    {'s' , "..."},{'t' , "-"},{'u' , "..-"},
                    {'v' , "...-"},{'w' , ".--"},{'x' , "-..-"},
                    {'y' , "-.--"},{'z' , "--.."},
                    //Numbers 
                    {'0' , "-----"},{'1' , ".----"},{'2' , "..----"},{'3' , "...--"},
                    {'4' , "....-"},{'5' , "....."},{'6' , "-...."},{'7' , "--..."},
                    {'8' , "---.."},{'9' , "----."},
                };      

     加密:

      加密的过程就是把已知的或者说要发送的转为Morse code。本文的例子中增加标识符另作它用,请忽略。  

         /// <summary>
            /// 加密
            /// </summary>
            /// <param name="value">命令</param>
            /// <returns>密文</returns>
            public string WordsTransferToMoresCodes(string value)
            {
                string values = "";
                if (value.Length < 0)
                {
                    return values;
                }           
                           
                foreach (char words in value.ToCharArray()) //拆分字符串为字节数组
                {
                    foreach (var dic in morseCode)
                    {
                        if (dic.Key == words)
                        {
                            values += dic.Value +"|"; //"|"为标识码
                        }
                    }                
                }
    
                return values;
            }

    解密:

         /// <summary>
            /// 解密
            /// </summary>
            /// <param name="code">密文</param>
            /// <returns>命令</returns>
            public string MoresCodeTransferToWords(string code)
            {
                string keys = "";
                if (code.Length < 0)
                {
                    return keys;
                }
                
                foreach (string codes in code.Split('|')) //拆分密文
                {
                    foreach (var dic in morseCode) //遍历mores集合
                    {
                        if (dic.Value == codes)
                        {
                            keys += dic.Key ;
                        }
                    }
                }
    
                return keys;
            }

    事情进行到这里,基本可以说是杀青了,当然,正如前文所讲,实现的部分不包括标点符号,非英语字符特殊字符,各位可各抒己见。

    好,就这样,放学。

    TymonYang   May-16 2015

  • 相关阅读:
    Compile Groovy/Spock with GMavenPlus
    Jenkins Slave Nodes – using the Swarm Plugin
    [NodeJS]Jenkins-cli
    [CoffeeScript]使用Yield功能
    [JavaScript]String.format
    [CoffeeScript]在WebStorm里运行CoffeeScript
    自动化运维的三阶段理论
    [Ruby]Unzipping a file using rubyzip
    测试文章引用
    敏捷软件测试读书笔记
  • 原文地址:https://www.cnblogs.com/tymonyang/p/4508174.html
Copyright © 2011-2022 走看看