Home > Backend Development > C#.Net Tutorial > Morse code C# implementation

Morse code C# implementation

大家讲道理
Release: 2016-11-10 09:54:12
Original
5109 people have browsed it

Morse code (also translated as Morse code, Morse code) is a signal code that is on and off, expressing different English letters, numbers and punctuation marks through different arrangements. It was invented by American Alfred Ware in 1837. Morse code is an early form of digital communication, but it is different from modern binary codes that only use two states: zero and one. Its codes include five types: Dots, dashes, pauses between dots and dashes, short pauses between each character (between dots and dashes), medium pauses between each word, and long pauses between sentences.

Morse code C# implementation

Note that only the content in ASCII encoding 127 delimiters such as English/numbers can be used and

Unicode is not supported, but you can modify part of it yourself to support Unicode

Sample code:

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;  
   
    }  
   
}
Copy after login

Use code:

string encry = MorseCode.Enc("China"); // 把China换成摩尔斯电码  
string decry = MorseCode.Dec(encry); //把encry换成明文形式
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template