Tabelle mit Asci zeichen wie anlegen (besten)

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
patrickk
User
Beiträge: 31
Registriert: Donnerstag 18. August 2005, 09:07

Danke fuer die Hilfe. konnte erst jetzt mir den Beitrag anschauen. Die tabelle kann man einfach aufgelistet anlegen. Ich habe jeden Eintrag mit : zugewiesen...
Nur die Dekodierung stimmt so nicht.
Es muss so gehen:

b7a1a2a3a4a5a6a7
c6c7b1b2b3b4b5b6
0 0 c1c2c3c4c5

das bit b7 muss rechts eingefuegt werden im naechsten Codewort. Im zweiten Schritt, dann die naechsten beiden links nach rechts fuers dritte... und achtung: Bei dem 7.ten schritt kaemen ja 7 in das naechste byte, was aber einem byte entspricht. Daher ist dies direkt das naecshte byte und danach geht es wieder von vorne los...
Das kann ich aber nicht mit shiften :-) Daher habe ich die zeichen nach hex gewandelt. Dann eine tabelle von hex nach bin, was ich als Zeichenkette angelegt habe. Und dort habe ich dann mit :... und tmp speicher rumgeschoben..
Deine Loesung waere aber viel eleganter!!

atrick
BlackJack

patrickk hat geschrieben:Nur die Dekodierung stimmt so nicht.
Okay, so sollte sie jetzt stimmen:

Code: Alles auswählen

from itertools import imap, cycle

def unpack_7bit(data):
    """Iterates over a string with 7 bit GSM packed data and returns integer
    values between 0 and 127.
    
    The data is treated as a bitstream where the values are encoded as blocks
    of eight values in seven bytes according to the following pattern:
    
    ----  --- -------- -------- -------- -------- -------- -------- --------
    Byte  ...       6        5        4        3        2        1        0
    ----  --- -------- -------- -------- -------- -------- -------- --------
    Bits  ... 77777776 66666655 55555444 44443333 33322222 22111111 10000000
    ----  --- -------- -------- -------- -------- -------- -------- --------
    
    The values are given in reverse order in the table.  The blocks of bits are
    in the correct order then, e.g the first value is simply the lower seven
    bits of byte 0 and the second is the MSB of byte 0 as LSB and the lower six
    bits of byte 1 as higher bits.  The values can be read right to left in the
    table above.
    
    :param data: the data as byte string.
    :type data: `str`
    
    :returns: an iterator over the values.
    :rtype: iterator of `int`
    """
    bytes = imap(ord, data)
    tmp = bytes.next()
    try:
        for i in cycle(xrange(8)):
            if i == 0:
                # 
                # The first value of each seven byte block is the only one
                # that is a) limited to one byte and b) doesn't have to be
                # shifted at all.
                # 
                result = tmp
            else:
                result = tmp >> (8 - i)
                tmp = bytes.next()
                result |= tmp << i
            yield result & 127
    except StopIteration:
        # 
        # If the last byte was an '8th' value then `result` contains this
        # value and has to be returned.
        # 
        if i == 7:
            yield result
Jetzt hatte ich auch eine kleine Testnachricht im Netz gefunden:

Code: Alles auswählen

def test():
    """A little test."""
    data = ('\xC8\x34\x28\xC8\x66\xBB\x40\x54\x74\x7A\x0E\x6A\x97\xE7\xF3\xF0'
            '\xB9\x0C\xBA\x87\xE7\xA0\x79\xD9\x4D\x07\xD1\xD1\xF2\x77\xFD\x8C'
            '\x06\x19\x5B\xC2\xFA\xDC\x05\x1A\xBE\xDF\xEC\x50\x08')
    print decode(data)
Antworten