Posts Tagged 'telex'

Baudot fun

This is a throwaway piece of code, so it’s probably buggy but what the heck!


#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys

zero = '○'
one = '●'

numbers = []

def int2bin(n, count=5):
    """returns the binary of integer n, using count number of digits"""
    return "".join([ zero if ((n >> y) & 1) == 0 else one for y in range(count-1, -1, -1)])

def write(code):
    print numbers

def output(newstate, curstate, code):

    figs = 27
    ltrs = 31

    if newstate == 0 and curstate == 1:
        write(ltrs)
    elif newstate == 1 and curstate == 0:
        write(figs)

    write(code)

    return newstate

def translate(input):
    global numbers

    numbers = map(int2bin,range(0,32))
    baudot_letters = '@E@A SIU@DRJNFCKTZLWHYPQOBG@MXV@'
    baudot_symbols = '@3@- @87@$4\',!:(5")2@6019?&@./;@'

    cr = 8
    lf = 2
    sp = 4
    state = 0

    for c in input.upper():
        if c == '@':
            continue
        if c == ' ':
            output(0,0,sp)
        elif c == '\n':
            output(0,0,cr)
            output(0,0,lf)
        else:
            ix = baudot_letters.find(c)
            if ix != -1:
                state = output(0, state, ix)
            else:
                ix = baudot_symbols.find(c)
                if ix != -1:
                    state = output(1, state, ix)

if __name__ == '__main__':
    for arg in sys.argv[1:]:
        translate(arg)

In case you're wondering, the Baudot code was invented in 1870 by Émile Baudot and later became the foundation of the international telex alphabets. See http://en.wikipedia.org/wiki/Baudot_code.

The original telex machines were electro-mechanical beasts which used this 5-bit code for wire transmission. Telex machines could encode text to punched tape for later sending. They could also print incoming telex transmissions to tape as well as paper. I remember seeing in a post bureau in Pakistan a journalist receiving a message on one telex machine and feeding the output spool of tape into another as it arrived. An early version of message forwarding!

Example output of the above:

johncc@liberator:~$ python baudot.py 'Hello world!'
●○●○○
○○○○●
●○○●○
●○○●○
●●○○○
○○●○○
●○○●●
●●○○○
○●○●○
●○○●○
○●○○●
●●○●●
○●●○●

Advertisements