October
---
2004




Morse code translator Webservice with ASP.NET

I have been planning on creating a little experiment with a .NET webservice, but never had the time to get around and do it. Here I took a simple concept from many years ago. Back in college, I had to learn by heart the morse code and be able to read(listen) and write fluently in this sound based language. Of course, that was then, and I don't remember much anymore, but thought I'd bring it back by creating a simple webservice that would accept a simple string as its only parameter and return the equivalent morse code. Nothing fancy, really.

You can try it out below. The service contains only one method, named msgToMorse, which takes one parameter of Type String.

Request   MsgToMorse(
    string =
  )

Response   

asmx.cs code

				
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace ws_morse{
 public class MsgToMorseByMxer: System.Web.Services.WebService{

 private ArrayList	alphabetList = new ArrayList();
 private ArrayList	morseList = new ArrayList();

 public MsgToMorseByMxer(){
  InitializeComponent();
 }

 #region Component Designer generated code
 private IContainer components = null;

 private void InitializeComponent(){
  this.alphabetList.AddRange	(new string[]{
   "a","b","c","d","e","f","g",
   "h","i","j","k","l","m","n",
   "o","p","q","r","s","t","u",
   "v","w","x","y","z"
  });

  this.morseList.AddRange	(new string[]{
   ".-","-...","-.-.","-..",".","..-.","--.",
   "....","..",".---","-.-",".-..","--","-.",
   "---",".--.","--.-",".-.","...","-","..-",
   "...-",".--","-..-","-.--","--.."
  });
 }

 protected override void Dispose( bool disposing ){
  if(disposing && components != null){
    components.Dispose();
  }
  base.Dispose(disposing);
 }

 #endregion

 [WebMethod]
 public string msgToMorse(string msg){
  string	returnValue	= null;
  char[]	charArray=((string)msg.ToLower()).ToCharArray(0, msg.Length);

  for(int i=0; i < msg.Length; i++){
   if(this.alphabetList.Contains(charArray[i].ToString())){
    returnValue += 
     morseList[this.alphabetList.IndexOf(charArray[i].ToString())].ToString();
   }
   returnValue += " ";
  }
  return returnValue;
 }
}