Skip to content

Instantly share code, notes, and snippets.

@davepeck
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davepeck/10428335 to your computer and use it in GitHub Desktop.
Save davepeck/10428335 to your computer and use it in GitHub Desktop.
Convert black-and-white sprites into... morse code?
Alien in base 16: 183C7EDBFF245AA5
Alien in base 32: 1GF3URFVI8ML5
Alien in base 36: D9NURFZQYYSL
Alien in morse, base 16: ['.----', '---..', '...--', '-.-.', '--...', '.', '-..', '-...', '..-.', '..-.', '..---', '....-', '.....', '.-', '.-', '.....']
Alien in morse, base 32: ['.----', '--.', '..-.', '...--', '..-', '.-.', '..-.', '...-', '..', '---..', '--', '.-..', '.....']
Alien in morse, base 36: ['-..', '----.', '-.', '..-', '.-.', '..-.', '--..', '--.-', '-.--', '-.--', '...', '.-..']
ALIEN = """
...XX...
..XXXX..
.XXXXXX.
XX.XX.XX
XXXXXXXX
..X..X..
.X.XX.X.
X.X..X.X
"""
def base(num, b, alphabet="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
"""
Return the integer 'num' in base 'b', using the alphabet provided.
"""
return ((num == 0) and alphabet[0]) or \
(base(num // b, b, alphabet).lstrip(alphabet[0]) + alphabet[num % b])
def sprite_to_num(sprite):
"""
Convert a sprite string (like ALIEN) into a single number
that represents it.
"""
# This uses a hack to convert it into a bit string that python's
# built-in int() will convert. Bits are base 2!
bits = sprite.replace("X", "1").replace(".", "0").replace("\n", "")
return int(bits, 2)
def sprite_base(sprite, b):
return base(sprite_to_num(sprite), b)
# CONVERT THE ALIEN FOR MORSE CODE TAPPING
print "Alien in base 16: ", sprite_base(ALIEN, 16) # TI 99/4A BASIC
print "Alien in base 32: ", sprite_base(ALIEN, 32) # GRAPH PAPER MORSE
print "Alien in base 36: ", sprite_base(ALIEN, 36) # BEST POSSIBLE MORSE
MORSE = {
'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': '--..',
'0': '-----',
'1': '.----',
'2': '..---',
'3': '...--',
'4': '....-',
'5': '.....',
'6': '-....',
'7': '--...',
'8': '---..',
'9': '----.',
}
def morse(s):
return [MORSE[c] for c in s]
def sprite_morse(sprite, b=36):
return morse(sprite_base(sprite, b))
# CONVERT AGAIN
print "Alien in morse, base 16: ", sprite_morse(ALIEN, 16) # TI 99/4A BASIC
print "Alien in morse, base 32: ", sprite_morse(ALIEN, 32) # GRAPH PAPER
print "Alien in morse, base 36: ", sprite_morse(ALIEN, 36) # BEST POSSIBLE
@davepeck
Copy link
Author

@davepeck
Copy link
Author

This was in response to my brother, who's a HAM radio op and wondered if there would be an easy way to transmit old-school sprites -- like on the TI 99/4A -- via morse code. (Yeah, it's a silly question, and here's my silly solution.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment