<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: morse code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 05:20:48 GMT</pubDate>
    <description>DZone Snippets: morse code</description>
    <item>
      <title>Morse decode using sed</title>
      <link>http://snippets.dzone.com/posts/show/5144</link>
      <description>This is a short &lt;a href="http://en.wikipedia.org/wiki/Morse_code"&gt;Morse code&lt;/a&gt; decoder written as a shellscript using &lt;a href="http://en.wikipedia.org/wiki/Sed"&gt;sed&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;The Morse coded text should be in $text, and should be written with spaces between the letters.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;echo $text\  | tr . 0 | sed -e {s/0----\ /1/g} -e {s/00---\ /2/g} -e {s/000--\ /3/g} -e {s/000-\ /4/g} -e {s/00000\ /5/g} -e {s/-0000\ /6/g} -e {s/--000\ /7/g} -e {s/---00\ /8/g} -e {s/----0\ /9/g} -e {s/-----\ /0/g} \&lt;br /&gt;	| sed -e {s/-0-0\ /c/g} -e {s/-000\ /b/g} -e {s/00-0\ /f/g} -e {s/0000\ /h/g} -e {s/0---\ /j/g} -e {s/0-00\ /l/g} -e {s/0--0\ /p/g} -e {s/--0-\ /q/g} -e {s/000-\ /v/g} -e {s/-00-\ /x/g} -e {s/-0--\ /y/g} -e {s/--00\ /z/g} \&lt;br /&gt;	| sed -e {s/0--\ /w/g} -e {s/-00\ /d/g} -e {s/--0\ /g/g} -e {s/-0-\ /k/g} -e {s/---\ /o/g} -e {s/0-0\ /r/g} -e {s/000\ /s/g} -e {s/00-\ /u/g} \&lt;br /&gt;	| sed -e {s/0-\ /a/g} -e {s/00\ /i/g} -e {s/--\ /m/g} -e {s/-0\ /n/g} \&lt;br /&gt;	| sed -e {s/0\ /e/g} -e {s/-\ /t/g}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 15 Feb 2008 13:54:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5144</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>Translates Morse code from a mobile phone style keypad into plain English</title>
      <link>http://snippets.dzone.com/posts/show/4612</link>
      <description>// Translates Morse code specially input from a Nokia 3510i or Nokia 6600 to plain English. I primarily used the button 1 for a dot, 2 for a dash and 5 for the terminator. I later extended the flexibility of the inputs by allowing 4 to be a dot, 7 a dash and 9 as the terminator. eg. ghi = dots pqrs = dashes wxyz = terminators and spaces. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;#filename: morsecode.rb&lt;br /&gt;&lt;br /&gt;require 'net/http'&lt;br /&gt;require 'rexml/document'&lt;br /&gt;&lt;br /&gt;include REXML&lt;br /&gt;&lt;br /&gt;class Morsecode&lt;br /&gt;  def initialize&lt;br /&gt;    @doc_mlookup = load_lookup('http://jamesrobertson.eu/p/xml/', 'morsecode.xml')&lt;br /&gt;    @doc_charmcode = load_lookup('http://jamesrobertson.eu/p/xml/', 'charmcode.xml')&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def load_lookup(url, filename)&lt;br /&gt;    xml_data = Net::HTTP.get_response(URI.parse(url + filename)).body&lt;br /&gt;    Document.new(xml_data)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def decode_string(doc, a_buffer)&lt;br /&gt;    pstring = ''&lt;br /&gt;    a_buffer.each do |word|&lt;br /&gt;      pstring += doc.root.elements["code[@encode='" + word + "']"].text&lt;br /&gt;    end&lt;br /&gt;    pstring&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def decode_kstring(buffer)&lt;br /&gt;    decode_string(@doc_charmcode, buffer.scan(/./))&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def decode_mstring(buffer)&lt;br /&gt;    decode_string(@doc_mlookup, buffer.split('4'))&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def decode(buffer)&lt;br /&gt;    decode_mstring(decode_kstring(buffer)) if buffer.length &gt; 0&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;#test&lt;br /&gt;a = Morsecode.new&lt;br /&gt;puts a.decode('gwpwrwgw')&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 03 Oct 2007 17:24:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4612</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
  </channel>
</rss>
