rendered paste body# iMelody helpersMAX_SMS_LENGTH = 140TYPE_MELODY = chr(0x0C)MELODY_HEADER_LENGTH = 30UDH_HEADER_LENGTH = 4def encode_melody(imelody_descriptor): result = [] imelody_source = ''.join(imelody_descriptor.splitlines()) if len(imelody_source) + MELODY_HEADER_LENGTH + UDH_HEADER_LENGTH > MAX_SMS_LENGTH: imelody_source = imelody_source[0: MAX_SMS_LENGTH - MELODY_HEADER_LENGTH - UDH_HEADER_LENGTH] gpos = 0 for i in ('1', '2', '3', '4', '5', '6'): pos = imelody_source.rfind(i) if pos > gpos: gpos = pos imelody_source = imelody_source[0:gpos] imelody_source = "'BEGIN:IMELODY\r\n" + imelody_source + "\r\nEND:IMELODY\r\n" imelody_length = len(imelody_source) result.append(str2binhex(chr(imelody_length + 3) + TYPE_MELODY + chr(imelody_length +1) + chr(00) + imelody_source)) def str2binhex(source): result = '' for c in source: result += result + "%.2x" % ord(c)