摩斯碼轉碼器

來源:互聯網
上載者:User

標籤:逆向   stat   運行   解碼   string   數組   技術   https   var   

1、背景

  今天是1024,程式員節那就幹點兒程式員的事情。剛好,記得上高中時候,看過一部電影,無間道,裡邊黃秋生和梁朝偉用摩斯碼通訊,瞬間覺得好神秘,好帥氣。最近閑來無事,出於對當初興趣的尊敬,就順手實現了一款簡易的摩斯轉碼器。

2、編解碼設計

  自己玩兒,暫訂中文摩斯編碼。基本思想是,將漢字對應的四位區位碼數字分別編碼為數字對應的摩斯碼,一個中文對應20位摩斯碼。解碼過程逆向。

3、代碼實現

  首先,項目結構圖如下:

  因為是個小工具,整個項目採用WPF實現。

主介面:

就核心過程而言,編碼分兩步:

1、漢字轉區位碼

 此演算法是直接抄的,原始出處,有點兒懶得費功夫找了,再次聲明,不是在下原創。

 /// <summary>        /// 漢字轉區位碼方法        /// </summary>        /// <param name="chinese">漢字</param>        /// <returns>區位碼</returns>        public static string ChineseToCoding(string chinese)        {            string pCode = "";            byte[] pArray = new byte[2];            pArray = Encoding.GetEncoding("GB2312").GetBytes(chinese);//得到漢字的位元組數組            int front = (short)(pArray[0] - ‘\0‘) - 160;//將位元組數組的第一位160            int back = (short)(pArray[1] - ‘\0‘) - 160;//將位元組數組的第二位160            pCode = front.ToString("D2") + back.ToString("D2");//再串連成字串就組成漢字區位碼            return pCode;        }

 

2、區位碼轉摩斯碼

 

private static readonly Dictionary<string, string> _dictNumberMorse = new Dictionary<string, string>        {            { "0", "— — — — —"},            { "1", "· — — — —"},            { "2", "· · — — —"},            { "3", "· · · — —"},            { "4", "· · · · —"},            { "5", "· · · · ·"},            { "6", "— · · · ·"},            { "7", "— — · · ·"},            { "8", "— — — · ·"},            { "9", "— — — — ·"},        };        private static readonly Dictionary<string, string> _dictMorseNumber = new Dictionary<string, string>        {            { "—————", "0"},            { "·————", "1"},            { "··———", "2"},            { "···——", "3"},            { "····—", "4"},            { "·····", "5"},            { "—····", "6"},            { "——···", "7"},            { "———··", "8"},            { "————·", "9"},        };        public static string GBK2Morse(string gbkCode)        {            if (string.IsNullOrWhiteSpace(gbkCode) || gbkCode.Length != 4)            {                throw new ArgumentException($"{nameof(gbkCode)}非GBK區位碼");            }            StringBuilder sbMorse = new StringBuilder();            foreach (var s in gbkCode)            {                sbMorse.Append(_dictNumberMorse[s.ToString()]).Append("     ");            }            return sbMorse.ToString();        }

 

4、運行效果 

github地址:https://github.com/KINGGUOKUN/MorseEncoder

5、結語

  /     · · · · ·     · · · · ·     — — — — —     · · · — —     /     · · — — —     — — — — —     — — — · ·     · · · — —     /     · · — — —     — — — · ·     · · · · ·     — — — — —     /1024/     · · · — —     · — — — —     — — · · ·     — · · · ·     /     · · · — —     · · — — —     · · · · ·     · · · · —     /!

(大家猜出結語是啥了嗎?)

 

摩斯碼轉碼器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.