IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Calcul scientifique Python Discussion :

Calcul de crc


Sujet :

Calcul scientifique Python

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Ingénieur développement matériel électronique
    Inscrit en
    Septembre 2015
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement matériel électronique

    Informations forums :
    Inscription : Septembre 2015
    Messages : 16
    Par défaut Calcul de crc
    Bonjour a tous,

    Je suis nouveau sur le python et j'essaie de faire du calcul de crc. J'ai installé le module crcmod mais j'arrive pooo à m'en servir...

    Qqn pourrait m'aider?

    Merci d'avance!
    François

  2. #2
    Expert éminent
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 790
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 790
    Par défaut
    Salut,

    Si vous voulez qu'on vous aide, c'est mieux de poster du code et décrire les problèmes rencontrés à le mettre au point: si quelqu'un à du temps et des idées, il pourra essayer de vous aider. Juste crier "à l'aide" ne sert à rien.

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  3. #3
    Membre confirmé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Octobre 2013
    Messages
    156
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Santé

    Informations forums :
    Inscription : Octobre 2013
    Messages : 156
    Par défaut
    Salut,

    Peut-être que ce code peut t'aider (Çà viens du module Pycrc):


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
     
    class Crc(object):
        """
        A base class for CRC routines.
        """
     
        # Class constructor
        ###############################################################################
        def __init__(self, width, poly, reflect_in, xor_in, reflect_out, xor_out, table_idx_width = None):
            """The Crc constructor.
     
            The parameters are as follows:
                width
                poly
                reflect_in
                xor_in
                reflect_out
                xor_out
            """
            self.Width          = width
            self.Poly           = poly
            self.ReflectIn      = reflect_in
            self.XorIn          = xor_in
            self.ReflectOut     = reflect_out
            self.XorOut         = xor_out
            self.TableIdxWidth  = table_idx_width
     
            self.MSB_Mask = 0x1 << (self.Width - 1)
            self.Mask = ((self.MSB_Mask - 1) << 1) | 1
            if self.TableIdxWidth != None:
                self.TableWidth = 1 << self.TableIdxWidth
            else:
                self.TableIdxWidth = 8
                self.TableWidth = 1 << self.TableIdxWidth
     
            self.DirectInit = self.XorIn
            self.NonDirectInit = self.__get_nondirect_init(self.XorIn)
            if self.Width < 8:
                self.CrcShift = 8 - self.Width
            else:
                self.CrcShift = 0
     
     
        # function __get_nondirect_init
        ###############################################################################
        def __get_nondirect_init(self, init):
            """
            return the non-direct init if the direct algorithm has been selected.
            """
            crc = init
            for i in range(self.Width):
                bit = crc & 0x01
                if bit:
                    crc^= self.Poly
                crc >>= 1
                if bit:
                    crc |= self.MSB_Mask
            return crc & self.Mask
     
     
        # function reflect
        ###############################################################################
        def reflect(self, data, width):
            """
            reflect a data word, i.e. reverts the bit order.
            """
            x = data & 0x01
            for i in range(width - 1):
                data >>= 1
                x = (x << 1) | (data & 0x01)
            return x
     
     
        # function bit_by_bit
        ###############################################################################
        def bit_by_bit(self, in_data):
            """
            Classic simple and slow CRC implementation.  This function iterates bit
            by bit over the augmented input message and returns the calculated CRC
            value at the end.
            """
            # If the input data is a string, convert to bytes.
            if isinstance(in_data, str):
                in_data = [ord(c) for c in in_data]
     
            register = self.NonDirectInit
            for octet in in_data:
                if self.ReflectIn:
                    octet = self.reflect(octet, 8)
                for i in range(8):
                    topbit = register & self.MSB_Mask
     
                    register = ((register << 1) & self.Mask) | ((octet >> (7 - i)) & 0x01)
     
                    if topbit:
                        register ^= self.Poly
     
     
            for i in range(self.Width):
                topbit = register & self.MSB_Mask
                register = ((register << 1) & self.Mask)
                if topbit:
                    register ^= self.Poly
     
            if self.ReflectOut:
                register = self.reflect(register, self.Width)
            return register ^ self.XorOut
     
     
        # function bit_by_bit_fast
        ###############################################################################
        def bit_by_bit_fast(self, in_data):
            """
            This is a slightly modified version of the bit-by-bit algorithm: it
            does not need to loop over the augmented bits, i.e. the Width 0-bits
            wich are appended to the input message in the bit-by-bit algorithm.
            """
            # If the input data is a string, convert to bytes.
            if isinstance(in_data, str):
                in_data = [ord(c) for c in in_data]
     
            register = self.DirectInit
            for octet in in_data:
                if self.ReflectIn:
                    octet = self.reflect(octet, 8)
                for i in range(8):
                    topbit = register & self.MSB_Mask
                    if octet & (0x80 >> i):
                        topbit ^= self.MSB_Mask
                    register <<= 1
                    if topbit:
                        register ^= self.Poly
                register &= self.Mask
            if self.ReflectOut:
                register = self.reflect(register, self.Width)
            return register ^ self.XorOut
     
     
        # function gen_table
        ###############################################################################
        def gen_table(self):
            """
            This function generates the CRC table used for the table_driven CRC
            algorithm.  The Python version cannot handle tables of an index width
            other than 8.  See the generated C code for tables with different sizes
            instead.
            """
            table_length = 1 << self.TableIdxWidth
            tbl = [0] * table_length
            for i in range(table_length):
                register = i
                if self.ReflectIn:
                    register = self.reflect(register, self.TableIdxWidth)
                register = register << (self.Width - self.TableIdxWidth + self.CrcShift)
                for j in range(self.TableIdxWidth):
                    if register & (self.MSB_Mask << self.CrcShift) != 0:
                        register = (register << 1) ^ (self.Poly << self.CrcShift)
                    else:
                        register = (register << 1)
                if self.ReflectIn:
                    register = self.reflect(register >> self.CrcShift, self.Width) << self.CrcShift
                tbl[i] = register & (self.Mask << self.CrcShift)
            return tbl
     
     
        # function table_driven
        ###############################################################################
        def table_driven(self, in_data):
            """
            The Standard table_driven CRC algorithm.
            """
            # If the input data is a string, convert to bytes.
            if isinstance(in_data, str):
                in_data = [ord(c) for c in in_data]
     
            tbl = self.gen_table()
     
            register = self.DirectInit << self.CrcShift
            if not self.ReflectIn:
                for octet in in_data:
                    tblidx = ((register >> (self.Width - self.TableIdxWidth + self.CrcShift)) ^ octet) & 0xff
                    register = ((register << (self.TableIdxWidth - self.CrcShift)) ^ tbl[tblidx]) & (self.Mask << self.CrcShift)
                register = register >> self.CrcShift
            else:
                register = self.reflect(register, self.Width + self.CrcShift) << self.CrcShift
                for octet in in_data:
                    tblidx = ((register >> self.CrcShift) ^ octet) & 0xff
                    register = ((register >> self.TableIdxWidth) ^ tbl[tblidx]) & (self.Mask << self.CrcShift)
                register = self.reflect(register, self.Width + self.CrcShift) & self.Mask
     
            if self.ReflectOut:
                register = self.reflect(register, self.Width)
            return register ^ self.XorOut

  4. #4
    Membre averti
    Homme Profil pro
    Ingénieur développement matériel électronique
    Inscrit en
    Septembre 2015
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement matériel électronique

    Informations forums :
    Inscription : Septembre 2015
    Messages : 16
    Par défaut
    Merci IP!
    Au cas où pour un autre qui rencontrerait le mm problème:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    s = a2b_hex("a731")
     
    crc16 = crcmod.predefined.Crc('xmodem')
    crc16.update(s)
    z=crc16.digest()
    print(z)
     
     
    crc32_func = crcmod.mkCrcFun(0x104c11db7, initCrc=0, xorOut=0xFFFFFFFF)
    a=hex(crc32_func('123456789'))
    print("a : " + a)
    Attention pour les crc predefined, on ne peut avoir la valeur du crc directement mais uniquement à l'aide de .update qui permet de "stocker" la valeur dans la fonction et qui est récupérée par .digest

    Ciao!

  5. #5
    Expert éminent
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 790
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 790
    Par défaut
    Citation Envoyé par fdesl Voir le message
    Au cas où pour un autre qui rencontrerait le mm problème:
    Quel problème?

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. recuperer (ou calculer le CRC d'un fichier)
    Par johannlb dans le forum VB.NET
    Réponses: 6
    Dernier message: 02/08/2007, 18h15
  2. Calcul de CRC
    Par boubajazz dans le forum Linux
    Réponses: 1
    Dernier message: 13/06/2007, 12h18
  3. calcul du CRC sur 2 octets
    Par jeannot27 dans le forum C++Builder
    Réponses: 6
    Dernier message: 19/12/2005, 11h55
  4. [SRC] Calcul de CRC
    Par cfdev dans le forum C++Builder
    Réponses: 3
    Dernier message: 07/03/2005, 14h08

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo