tags:

views:

296

answers:

2

Is there a python library for encoding ascii data to 7-bit GSM character set (for sending SMS)?

-- mks --

A: 

I could not find any library. But I think this should not need a library. Its somewhat easy to do.

Here is Jon Skeet himself on the same topic.

Example:

s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def ascii_to_gsm(ch):
    return bin(65 + s.index(ch))

print ascii_to_gsm('A')
print '--'

binary_stream = ''.join([str(ascii_to_gsm(ch))[2:] for ch in s])
print binary_stream

You can also use dict to store mapping between ASCII and GSM 7-bit character set.

TheMachineCharmer
+4  A: 

There is now :)

# -*- coding: utf8 -*- 
gsm = "@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\x1bÆæßÉ !\"#¤%&'()*=,-./0123456789:;<=>?"
gsm +="¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑÜ`¿abcdefghijklmnopqrstuvwxyzäöñüà"
ext = "````````````````````^```````````````````{}`````\\````````````[~]`"
ext +="|````````````````````````````````````€``````````````````````````"

def gsm_encode(plaintext):
    res=""
    for c in plaintext:
        idx = gsm.find(c);
        if idx != -1:
            res += chr(idx)
            continue
        idx = ext.find(c)
        if idx != -1:
            res += chr(27)
            res += chr(idx)
    return res.encode('hex')

print gsm_encode("Hello World")

The output is hex. Obviously you can skip that if you want the binary stream

gnibbler
plus one good one :)
TheMachineCharmer