• 0

Morse Code


Question

6 answers to this question

Recommended Posts

  • 0

That code does quite a bit more than translates to/from morse though, such as generating sounds to play.

Anyway, one way of doing it in C# is simply to make two System.Collections.Hashtable structures, one mapping morse to letters, and the other the reverse. Then make a morse text and english text parser and make lookups in the hashtables and you're done.

I could do a simple example to show what I'm talking about in code, but don't have time for that right now.

Link to comment
https://www.neowin.net/forum/topic/397764-morse-code/#findComment-586813365
Share on other sites

  • 0
  Jugalator said:
That code does quite a bit more than translates to/from morse though, such as generating sounds to play.

Anyway, one way of doing it in C# is simply to make two System.Collections.Hashtable structures, one mapping morse to letters, and the other the reverse. Then make a morse text and english text parser and make lookups in the hashtables and you're done.

I could do a simple example to show what I'm talking about in code, but don't have time for that right now.

586813365[/snapback]

-------------------------------------------------------------------------------------------

My bad luck, I wish you could spare some of your valuable time to show me with a simple example as you mentioned, the complete source code in java is available at http://morsecode.scphillips.com/Morse.java

I DON'T NEED THE SOUND PORTION OF THIS PROGRAM, PLEASE...

Link to comment
https://www.neowin.net/forum/topic/397764-morse-code/#findComment-586816640
Share on other sites

  • 0

well i know you didnt need the sound portion but you can search the code and find this handy block of code to help out with a hashtable:

/**************************************************************/
        /*  Text Processing                                          */
        /**************************************************************/

        public boolean isText(String sText) {
            boolean matches = false;
            try {
                RE morseRE = new RE("^[._/ \\|-]*$");
                matches = morseRE.isMatch(sText);
            } 
            catch (REException arg) { }
            return !matches;
        }

        public String toMorse(String sText) {
            /*
              tr/a-z/A-Z/; #lowercase
              tr/ / /s; #sqeeze spaces
              s/^ *(.*?) *$/$1/; #chop start and end of ' '
              s# #@ #g; #mark word boundaries (with non-Morse character)
              s#([A-Z0-9.,:?'-/()"])#$toMorse{$1}.' '#eg; #put Morse in
              s#@#/#g; #re-mark word boundaries
            */

            StringBuffer sbMorse = new StringBuffer();

            sText = sText.toUpperCase();
            try {
                RE sub1 = new RE("\\s+");
                RE sub2 = new RE("^\\s+");
                RE sub3 = new RE("\\s+$");
                //RE sub4 = new RE(" ");
                RE sub5 = new RE("[^A-Z0-9.,?\'\"/() -=@]");
                sText = sub1.substituteAll(sText, " ");
                sText = sub2.substitute(sText, "");
                sText = sub3.substitute(sText, "");
                sText = sub5.substituteAll(sText, "");
                input.setText(sText);

                for (int i = 0; i < sText.length(); i++) {
                    char c = sText.charAt(i);
                    Character ch = new Character(c);
                    if (Character.isSpace(c)) {
                        sbMorse.append("/ ");
                    } else {
                        sbMorse.append(hMorse.get(ch));
                        sbMorse.append(' ');
                    }
                }
            } catch (REException arg) {
                //input.setText(arg.getMessage());
                input.setText("error");
            }

            sText = sbMorse.toString();
            return sText;
        }

        public String toText(String sMorse) {
            /*
              tr/ / /s; #sqeeze space
              s#( ?/ ?)+# /#g; #deal with multiple and space-padded '/'
              s#^[ /]*##; #chop start of '/' and ' '
              s#[ /]*$# #; #chop end of same, leaving one ' '
              s#/#@#g; #mark word boundaries (with non-Morse character)
              s/([-.]+) /$fromMorse{"$1"}/eg; #put in text
              s/@/ /g; #replace word boundaries
              s#\(([^(]*)\(#\($1\)#g; #match brackets
            */
            StringBuffer sbText = new StringBuffer();
            String letter = new String();

            try {
                RE sub1 = new RE("\\|");  // for '/'
                RE sub2 = new RE("_");  // for '-'
                RE sub3 = new RE("\\s+");  // for ' '
                RE sub4 = new RE("( ?/ ?)+");  // for ' / '
                RE sub5 = new RE("^[ /]*");  // for ''
                RE sub6 = new RE("[ /]*$");  // for ''
                sMorse = sub1.substituteAll(sMorse, "/");
                sMorse = sub2.substituteAll(sMorse, "-");
                sMorse = sub3.substituteAll(sMorse, " ");
                sMorse = sub4.substituteAll(sMorse, " / ");
                sMorse = sub5.substitute(sMorse, "");
                sMorse = sub6.substitute(sMorse, "");
                input.setText(sMorse);
                sMorse = sMorse + " ";

                RE sub7 = new RE("^.*? ");  // for ''  (rest of string)
                RE sub8 = new RE(" .*$");  // for ''  (first word)

                while (sMorse.length() != 0) {
                    letter = sub8.substitute(sMorse, "");
                    sMorse = sub7.substitute(sMorse, "");
                    if (letter.equals("/")) {
                        sbText.append(" ");
                    } else if (hText.get(letter) == null) {
                        sbText.append("*");
                    } else {
                        sbText.append(hText.get(letter));
                    }
                }
                // need to do parentheses matching!

            } catch (REException arg) {
                //input.setText(arg.getMessage());
                input.setText("error");
            }

            sMorse = sbText.toString();
            return sMorse;
        }

You dont have to convert it line by line (but you can try). But its a good start/hint for your pseudo-code and thats the best way to learn.

But if you want another example i did a search on google "morse code C#"

and found this, the code project the best place to ask C# questions

http://www.codeproject.com/csharp/custstrformat.asp

he has the code to convert from text to morse code ( i dont know the other way around)

Link to comment
https://www.neowin.net/forum/topic/397764-morse-code/#findComment-586818134
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Saw this on Twitter/X. Can't imagine the emotional rollercoaster going on inside of her head.
    • It's telling how brainwashed  and triggered you are that you immediately think it's because he was MAGA.    There's tons of things he's done throughout his wrestling career alone that wrestling fans right wing/ or left wing have problems with.  Not to mention his personal life. 
    • UK enforces strict new online age checks today by Paul Hill As of today, July 25, 2025, new Ofcom regulations mandate that “highly effective” age checks are in place for online services. These new rules apply to any websites or platforms that host pornography, self-harm, suicide, or eating disorder content. Major platforms like Pornhub, Bluesky, Discord, Grindr, Reddit, and X have committed to implementing age-gating. These age checks are part of the broader Online Safety Act, which is designed to make the internet safer, particularly for kids. These measures move away from just confirming you’re 18 by clicking a button to having to actually verify your age with ID or a face scan, but this move is not without its critics. Starting today, Ofcom will actively check compliance with the new rules and start launching investigations into non-compliant services, starting next week. The current enforcement program that’s focused on studio porn services is extending to all platforms allowing user-shared pornographic material, not just those websites dedicated to that. Ofcom is also launching another enforcement program that will target websites specifically dedicated to harmful content like self-harm, suicide, eating disorders, and extreme violence/gore. Ofcom has strong enforcement powers under the Online Safety Act, it can dish out fines of up to 10% of qualifying worldwide revenue or £18 million. For the worst offenders, Ofcom can even get websites blocked in the UK. Ofcom is already investigating 11 companies that it doesn’t think are following the rules. Aside from age checks, Ofcom’s Codes also require websites to protect children from dangerous stunts, misogynistic, violent, hateful, or abusive material, and online bullying. Algorithms of social media will need to be configured to block harmful content in children’s feeds, for example. Ofcom will be launching an extensive monitoring program requiring risk assessments by August 7 and practical action disclosures by September 30. While some have criticized the Online Safety Act, research cited by Ofcom shows that 71% of UK parents think the changes will positively impact children’s online safety, with 77% being optimistic about the age checks specifically. With that said, a significant minority of parents (41%) are skeptical about whether tech firms will follow the rules. Source: Ofcom | Image via Depositphotos.com
    • Microsoft has known to toggle settings in Windows Updates.
    • Another Linux utility is being rewritten in Rust by David Uzondu Greenboot, the health check tool originally written in bash, is getting a rewrite in Rust, courtesy of engineers at Red Hat. This useful tool started in mid‑2018 as a Google Summer of Code project for Fedora IoT, designed to keep atomically updated systems from self-destructing after a bad update. At its heart, Greenboot is a framework that hooks into systemd to run health checks every time a machine boots. It looks for scripts in specific directories; anything in /etc/greenboot/check/required.d/ absolutely must pass. If a required script fails, Greenboot triggers a reboot to retry. After a few failed attempts, it executes scripts in /etc/greenboot/red.d/ and initiates a system rollback to the last known-good deployment, preventing an update from bricking your system. When all required checks succeed, it runs scripts from /etc/greenboot/green.d/ and marks the boot as successful by setting a GRUB environment variable. This whole process is kicked off by the greenboot-healthcheck.service before systemd's normal boot-complete.target is reached. As for why Red Hat is choosing this rewrite, it comes down to creating a more robust and secure utility. This is definitely not the only *-rs tool rewrite we have seen lately; you have probably heard about sudo-rs, which is a project to build a memory-safe replacement for the classic sudo utility. Building these fundamental system components in a memory-safe language like Rust helps eliminate entire categories of security vulnerabilities. According to the official Fedora change proposal, the rewrite expands support for both bootc and rpm-ostree based systems, whereas the original Bash version was built only for rpm-ostree. Red Hat developers have submitted a proposal to ship this new Rust version in Fedora 43. According to Phoronix, while the plan still needs a final vote from the Fedora Engineering and Steering Committee, it looks very likely to be approved. For current Fedora IoT users, the change promises to be a simple, seamless upgrade.
  • Recent Achievements

    • Very Popular
      d4l3d earned a badge
      Very Popular
    • Dedicated
      Stephen Leibowitz earned a badge
      Dedicated
    • Dedicated
      Snake Doc earned a badge
      Dedicated
    • One Month Later
      Philsl earned a badge
      One Month Later
    • One Month Later
      armandointerior640 earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      625
    2. 2
      ATLien_0
      240
    3. 3
      Xenon
      163
    4. 4
      +FloatingFatMan
      124
    5. 5
      neufuse
      123
  • Tell a friend

    Love Neowin? Tell a friend!