首頁 >後端開發 >C#.Net教程 >摩斯密碼(Morse code) C#實現

摩斯密碼(Morse code) C#實現

大家讲道理
大家讲道理原創
2016-11-10 09:54:125111瀏覽

摩斯電碼(又譯為摩斯密碼,Morse code)是一種時通時斷的訊號代碼,透過不同的排列順序來表達不同的英文字母、數字和標點符號。它是由美國人艾爾菲德·維爾於1837年發明。 摩斯電碼是一種早期的數位化通訊形式,但是它不同於現代只使用零和一兩種狀態的二進位代碼,它的代碼包括五種: 點、劃、點和劃之間的停頓、每個字符間短的停頓(在點和劃之間)、每個詞之間中等的停頓以及句子之間長的停頓。

摩斯密碼(Morse code) C#實現

注意只可以使用英文/數字等ASCII編碼127界限符中的內容並

不支持Unicode不過你可以自己修改一部分用於支持Unicode

示例代碼:不支持Unicode不過你可以自己修改一部分用於支持Unicode

示例代碼:

public static class MorseCode // 摩尔斯电码(星际穿越)  
   
{  
   
   
    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"}  
   
    };  
   
   
   
   
    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;  
   
    }  
   
   
   
   
    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 "{#}";  
   
    }  
   
   
   
   
    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;  
   
    }  
   
}


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn