def mangleNec(code, freq=40):
    """Convert NEC code to shorthand notation"""
    # base time is 550 microseconds
    # unit of burst time
    # lead in pattern:   214d 10b3
    # "1" burst pattern: 0226 0960
    # "0" burst pattern: 0226 0258
    # lead out pattern:  0226 2000
    # there's large disagreement between devices as to a common preamble
    # or the "long" off period for the representation of a binary 1
    # thus we can't construct a code suitable for transmission
    # without more information--but it's good enough for creating
    # a shorthand representaiton for use with recv
    timings = []
    for octet in binascii.unhexlify(code.replace(" ", "")):
        burst = lambda x: x and "0226 06AD" or "0226 0258"
        for bit in reversed("%08d" % int(bin(ord(octet))[2:])):
            bit = int(bit)
            timings.append(burst(bit))
    return mangleIR("K %0X22 214d 10b3 " % freq + " ".join(timings) + " 0226 2000")
Convert NEC code to shorthand notation