Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Morse Code as binary

by serf (Chaplain)
on Jan 19, 2006 at 20:20 UTC ( [id://524337]=CUFP: print w/replies, xml ) Need Help??

After looking at ambrus' cool text to morse one-liner this morning:

Alphabetic->morse converter:

perl -wpe 's{[a-z ]}{"% etianmsurwdkgohvf%l%pjbxcyzq"=~/\l$&/;(unpack" + +B8",pack"C",@-)=~/0+1(.*)/;($x="$1")=~y/01/.-/;$x}gie'
I have been having a play with the idea of representing morse code in binary and came up with this:
#!/usr/bin/perl # # Morse Code as binary # # Given this table: # http://en.wikipedia.org/wiki/Morse_code # #Alternate_display_of_more_common_characters_for_the_international_c +ode # # Building an array padded with ~ (which is not used in Morse) # this turns the array index into a binary number representing # the Morse code: # the first 3 digits are the number of dits and dahs in the letter # the last 5 digits represent the morse symbol where 0 is . and 1 is _ # use strict; use warnings; my @chars = qw( E T I A N M S U R W D K G O H V F Ü L Ä P J B X C Y Z Q Ö CH 5 4 ~ 3 É ~ Ð 2 ~ È + ~ Þ À ~ 1 6 = / ~ ~ ~ ~ ~ 7 ~ ~ Ñ 8 ~ 9 0 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ? _ ~ ~ ~ ~ " ~ ~ . ~ ~ ~ ~ @ ~ ~ ~ ' ~ ~ - ~ ~ ~ ~ ~ ~ ~ ~ ; ! ~ () ~ ~ ~ ~ ~ % ~ ~ ~ ~ : ~ ~ ~ ~ ~ ~ ~ ); $chars[113] = ','; # Rather than having ',' in the qw() array. # These are the indices where the number of characters increase. my %steps; map( $steps{$_} = 1, qw(1 3 7 15 31) ); my ($step, $fig); for my $idx ( 1 .. $#chars ) { $chars[$idx] =~ s/^~$//g; if ( $steps{$idx} ) { $fig += 32; $step = $fig - $idx; } printf "%08b %s\n", $idx + $step, $chars[$idx-1]; }
Which could also be used as the basis for a Text->Morse / Morse->text convertor.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://524337]
Approved by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-25 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found