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

C++Builder Discussion :

codes crc


Sujet :

C++Builder

  1. #1
    Candidat au Club
    Inscrit en
    Juillet 2002
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Juillet 2002
    Messages : 3
    Points : 2
    Points
    2
    Par défaut codes crc
    j'aimerais savoir si il esxiste un composant ou une classe qui permet de calculer le code crc d'un fichier
    merci

  2. #2
    Nouveau Candidat au Club
    Inscrit en
    Juin 2002
    Messages
    1
    Détails du profil
    Informations forums :
    Inscription : Juin 2002
    Messages : 1
    Points : 1
    Points
    1
    Par défaut algo CRC
    Salut,
    j'ai longtemps cherche ce type de composant sans jamais le trouver, donc j'ai fini par faire mon programme qui calcul le CRC pour le protocol Modbus, Je rentre chaque chiffre du protocol dans un Edit en Hexa, puis je calcul. voici mon code
    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
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
     
    //declaration
    AnsiString RESULT1,RESULT2,RESULT3,RESULT4;
    int CRC1=0,CRC2=0,CRC3=0,CRC4=0; // 4 quartets finals
    int i,N=0,fin=0;
    AnsiString MSB,LSB;
    bool tab2[16]; //octet en cours
    bool CRC[16];
    bool tab1[16]; //contient A001
    bool CRCbis[16];
    bool Flag; // flag pour la retenu lors du decalage
     
    for(i=0;i<16;i++) tab1[i]=0;
    tab1[15]=1,tab1[0]=1,tab1[2]=1;
     
    //initialisation CRC=FFFF :
    for(i=0;i<16;i++) CRC[i]=1;
     
    //inserer l'octet en cours
     
    while (fin!=6)
    {
    if (fin==0)
            {
            MSB = Edit6->Text;
            LSB = Edit7->Text;
            }
            else if (fin==1)
                    {
                    MSB = Edit8->Text;
                    LSB = Edit9->Text;
                    }
            else if (fin==2)
                    {
                    MSB = Edit10->Text;
                    LSB = Edit11->Text;
                    }
            else if (fin==3)
                    {
                    MSB = Edit12->Text;
                    LSB = Edit13->Text;
                    }
             else if (fin==4)
                    {
                    MSB = Edit14->Text;
                    LSB = Edit15->Text;
                    }
             else if (fin==5)
                    {
                    MSB = Edit16->Text;
                    LSB = Edit17->Text;
                    }
     
    if (MSB=="0")
                    {
                    for(i=0;i<16;i++) tab2[i]=0;
                    }
            else if (MSB=="1")
                    {
                    for(i=0;i<16;i++) tab2[i]=0;
                    tab2[11]=1;
                    }
            else if (MSB=="2")
                    {
                    for(i=0;i<16;i++) tab2[i]=0;
                    tab2[10]=1;
                    }
            else if (MSB=="3")
                    {
                    for(i=0;i<16;i++) tab2[i]=0;
                    tab2[10]=1,tab2[11]=1;
                    }
            else if (MSB=="4")
                    {
                    for(i=0;i<16;i++) tab2[i]=0;
                    tab2[9]=1;
                    }
            else if (MSB=="5")
                    {
                    for(i=0;i<16;i++) tab2[i]=0;
                    tab2[9]=1,tab2[11]=1;
                    }
            else if (MSB=="6")
                    {
                    for(i=0;i<16;i++) tab2[i]=0;
                    tab2[9]=1,tab2[10]=1;
                    }
            else if (MSB=="7")
                    {
                    for(i=0;i<16;i++) tab2[i]=0;
                    tab2[9]=1,tab2[10]=1,tab2[11]=1;
                    }
            else if (MSB=="8")
                    {
                    for(i=0;i<16;i++) tab2[i]=0;
                    tab2[8]=1;
                    }
            else if (MSB=="9")
                    {
                    for(i=0;i<16;i++) tab2[i]=0;
                    tab2[8]=1,tab2[11]=1;
                    }
            else if (MSB=="a")
                    {
                    for(i=0;i<16;i++) tab2[i]=0;
                    tab2[8]=1,tab2[10]=1;
                    }
            else if (MSB=="b")
                    {
                    for(i=0;i<16;i++) tab2[i]=0;
                    tab2[8]=1,tab2[10]=1,tab2[10]=1;
                    }
            else if (MSB=="c")
                    {
                    for(i=0;i<16;i++) tab2[i]=0;
                    tab2[8]=1,tab2[9]=1;
                    }
            else if (MSB=="d")
                    {
                    for(i=0;i<16;i++) tab2[i]=0;
                    tab2[8]=1,tab2[9]=1,tab2[11]=1;
                    }
            else if (MSB=="e")
                    {
                    for(i=0;i<16;i++) tab2[i]=0;
                    tab2[8]=1,tab2[9]=1,tab2[10]=1;
                    }
            else if (MSB=="f")
                    {
                    for(i=0;i<16;i++) tab2[i]=0;
                    tab2[8]=1,tab2[9]=1,tab2[10]=1,tab2[11]=1;
                    }
    //**********************************************
            if (LSB=="1")
                    {
                    tab2[15]=1;
                    }
                    else if (LSB=="2")
                            {
                            tab2[14]=1;
                            }
                    else if (LSB=="3")
                            {
                            tab2[14]=1,tab2[15]=1;
                            }
                    else if (LSB=="4")
                            {
                            tab2[13]=1;
                            }
                    else if (LSB=="5")
                            {
                            tab2[13]=1,tab2[15]=1;
                            }
                    else if (LSB=="6")
                            {
                            tab2[13]=1,tab2[14]=1;
                            }
                    else if (LSB=="7")
                            {
                            tab2[13]=1,tab2[14]=1,tab2[15]=1;
                            }
                    else if (LSB=="8")
                            {
                            tab2[12]=1;
                            }
                    else if (LSB=="9")
                            {
                            tab2[12]=1,tab2[15]=1;
                            }
                    else if (LSB=="a")
                            {
                            tab2[12]=1,tab2[14]=1;
                            }
                    else if (LSB=="b")
                            {
                            tab2[12]=1,tab2[14]=1,tab2[15]=1;
                            }
                    else if (LSB=="c")
                            {
                            tab2[12]=1,tab2[13]=1;
                            }
                    else if (LSB=="d")
                            {
                            tab2[12]=1,tab2[13]=1,tab2[15]=1;
                            }
                    else if (LSB=="e")
                            {
                            tab2[12]=1,tab2[13]=1,tab2[14]=1;
                            }
                    else if (LSB=="f")
                            {
                            tab2[12]=1,tab2[13]=1,tab2[14]=1,tab2[15]=1;
                            }
            fin=fin+1;
         //CRC = CRC XOR OCTET :
            for(i=0;i<16;i++) CRC[i] = CRC[i]^tab2[i];
            N=1;
            while (N<9)
                    {
                    //decalage a droite :
                    Flag=CRC[15]; // Sauvegarde du bit de poid faible
                    for(i=0;i<15;i++) CRCbis[i+1]=CRC[i];
                    CRCbis[0]=0; // Restitution du poid fort
                    // restitution du CRC
                    for(i=0;i<16;i++) CRC[i]=CRCbis[i];
                    //test sur la retenue :
                    if (Flag==1)
                            {
                            //CRC = CRC OU EXCLUSIF A001
                            for(i=0;i<16;i++) CRC[i]=CRC[i]^tab1[i];
                            }
                    N=N+1;
                    }
    }
    //**********************************************
    //passage binaire->decimal
    if (CRC[15]==1) CRC1=CRC1+1;
    if (CRC[14]==1) CRC1=CRC1+2;
    if (CRC[13]==1) CRC1=CRC1+4;
    if (CRC[12]==1) CRC1=CRC1+8;
     
    if (CRC[11]==1) CRC2=CRC2+1;
    if (CRC[10]==1) CRC2=CRC2+2;
    if (CRC[9]==1)  CRC2=CRC2+4;
    if (CRC[8]==1)  CRC2=CRC2+8;
     
    if (CRC[7]==1) CRC3=CRC3+1;
    if (CRC[6]==1) CRC3=CRC3+2;
    if (CRC[5]==1) CRC3=CRC3+4;
    if (CRC[4]==1) CRC3=CRC3+8;
     
    if (CRC[3]==1) CRC4=CRC4+1;
    if (CRC[2]==1) CRC4=CRC4+2;
    if (CRC[1]==1) CRC4=CRC4+4;
    if (CRC[0]==1) CRC4=CRC4+8;
     
    //passage binaire->hexadecimal :
    RESULT1= IntToHex(StrToInt(CRC1),1);
    RESULT2= IntToHex(StrToInt(CRC2),1);
    RESULT3= IntToHex(StrToInt(CRC3),1);
    RESULT4= IntToHex(StrToInt(CRC4),1);
    //Afichage du resultat :
    Edit18->Text = RESULT2;
    Edit19->Text = RESULT1;
    Edit20->Text = RESULT4;
    Edit21->Text = RESULT3;
    Button2->Enabled=true;
    Button1->Enabled=false;
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm3::Edit6KeyPress(TObject *Sender, char &Key)
    {
    if((Key < '0') || (Key > 'f'))      Key = 0;
     
    if((Key > ':') && (Key < '@'))      Key = 0;
     
    if((Key > 'H') && (Key < '`'))      Key = 0;
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm3::Edit7KeyPress(TObject *Sender, char &Key)
    {
    if((Key < '0') || (Key > 'f'))      Key = 0;
     
    if((Key > ':') && (Key < '@'))      Key = 0;
     
    if((Key > 'H') && (Key < '`'))      Key = 0;
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm3::Edit8KeyPress(TObject *Sender, char &Key)
    {
    if((Key < '0') || (Key > 'f'))      Key = 0;
     
    if((Key > ':') && (Key < '@'))      Key = 0;
     
    if((Key > 'H') && (Key < '`'))      Key = 0;        
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm3::Edit9KeyPress(TObject *Sender, char &Key)
    {
    if((Key < '0') || (Key > 'f'))      Key = 0;
     
    if((Key > ':') && (Key < '@'))      Key = 0;
     
    if((Key > 'H') && (Key < '`'))      Key = 0;        
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm3::Edit10KeyPress(TObject *Sender, char &Key)
    {
    if((Key < '0') || (Key > 'f'))      Key = 0;
     
    if((Key > ':') && (Key < '@'))      Key = 0;
     
    if((Key > 'H') && (Key < '`'))      Key = 0;        
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm3::Edit11KeyPress(TObject *Sender, char &Key)
    {
    if((Key < '0') || (Key > 'f'))      Key = 0;
     
    if((Key > ':') && (Key < '@'))      Key = 0;
     
    if((Key > 'H') && (Key < '`'))      Key = 0;        
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm3::Edit12KeyPress(TObject *Sender, char &Key)
    {
    if((Key < '0') || (Key > 'f'))      Key = 0;
     
    if((Key > ':') && (Key < '@'))      Key = 0;
     
    if((Key > 'H') && (Key < '`'))      Key = 0;        
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm3::Edit13KeyPress(TObject *Sender, char &Key)
    {
    if((Key < '0') || (Key > 'f'))      Key = 0;
     
    if((Key > ':') && (Key < '@'))      Key = 0;
     
    if((Key > 'H') && (Key < '`'))      Key = 0;        
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm3::Edit14KeyPress(TObject *Sender, char &Key)
    {
    if((Key < '0') || (Key > 'f'))      Key = 0;
     
    if((Key > ':') && (Key < '@'))      Key = 0;
     
    if((Key > 'H') && (Key < '`'))      Key = 0;        
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm3::Edit15KeyPress(TObject *Sender, char &Key)
    {
    if((Key < '0') || (Key > 'f'))      Key = 0;
     
    if((Key > ':') && (Key < '@'))      Key = 0;
     
    if((Key > 'H') && (Key < '`'))      Key = 0;        
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm3::Edit16KeyPress(TObject *Sender, char &Key)
    {
    if((Key < '0') || (Key > 'f'))      Key = 0;
     
    if((Key > ':') && (Key < '@'))      Key = 0;
     
    if((Key > 'H') && (Key < '`'))      Key = 0;        
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TForm3::Edit17KeyPress(TObject *Sender, char &Key)
    {
    if((Key < '0') || (Key > 'f'))      Key = 0;
     
    if((Key > ':') && (Key < '@'))      Key = 0;
     
    if((Key > 'H') && (Key < '`'))      Key = 0;
    }
    //---------------------------------------------------------------------------
    En esperant t'aider un peu...
    tu peux consulter l'algo sur :
    http://membres.lycos.fr/titicoco101/page6.html#ancre96826
    a ton service
    Chrisfly

  3. #3
    Modérateur

    Avatar de Vincent PETIT
    Homme Profil pro
    Consultant en Systèmes Embarqués
    Inscrit en
    Avril 2002
    Messages
    3 191
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Consultant en Systèmes Embarqués
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Avril 2002
    Messages : 3 191
    Points : 11 577
    Points
    11 577
    Par défaut
    Je pense chrisfly que ton début de code peut être amélioré.
    Par exemple en remplaçant tous tes if et else par des switch et case
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    switch (fin)
      { 
        case 0:MSB = Edit6->Text;LSB = Edit7->Text;break;
        case 1: .......etc....
      }
    enfin c'est juste une suggestion
    La science ne nous apprend rien : c'est l'expérience qui nous apprend quelque chose.
    Richard Feynman

  4. #4
    Membre à l'essai
    Inscrit en
    Avril 2002
    Messages
    13
    Détails du profil
    Informations forums :
    Inscription : Avril 2002
    Messages : 13
    Points : 14
    Points
    14
    Par défaut
    de meme Pour tout les KeyPress, avec un <dynamic-cast> sur le Sender. Pour plus de détail, cf la FAQ de Géronimo.
    Ginnovy

  5. #5
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2002
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2002
    Messages : 13
    Points : 15
    Points
    15
    Par défaut
    Il est horrible ce code !! Ça fait peur !!

    >ginnovy : pas besoin de dynamic_cast, il suffit de faire pointer à l'aide de l'inspecteur d'objet tous les OnKeyPress vers 1 méthode.

  6. #6
    Candidat au Club
    Inscrit en
    Juillet 2002
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Juillet 2002
    Messages : 3
    Points : 2
    Points
    2
    Par défaut
    arf, c pas ca qu'il me faut en fait (enfin je pense), j'aimerais faire un programme du genre d'un sfvchecker

  7. #7
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2002
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2002
    Messages : 13
    Points : 15
    Points
    15
    Par défaut
    Citation Envoyé par patturbo
    arf, c pas ca qu'il me faut en fait (enfin je pense), j'aimerais faire un programme du genre d'un sfvchecker
    C'était à parier que tu voulais pas ce genre de CRC mais j'dois dire qu'il a bien fait de poster son code parce que t'as question était on n'peut plus vague (CRC-16, CRC-16/CCITT, CRC32, ...)

    Pour sfvchecker, t'auras l'algo dans ces sources..

  8. #8
    Candidat au Club
    Inscrit en
    Juillet 2002
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Juillet 2002
    Messages : 3
    Points : 2
    Points
    2
    Par défaut
    c'est vrai que c'etait un peu vague merci je v essayer de me debrouiller avec ca

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

Discussions similaires

  1. [Toutes versions] [code fini] verification d erreur crc
    Par poechethc86 dans le forum Contribuez
    Réponses: 0
    Dernier message: 09/03/2012, 04h17
  2. De la rapidité du code
    Par jfloviou dans le forum Contribuez
    Réponses: 233
    Dernier message: 29/05/2009, 02h17
  3. Cherche l'algo crc 16 bits
    Par icepower dans le forum Algorithmes et structures de données
    Réponses: 2
    Dernier message: 21/08/2002, 13h27
  4. Explorateur de code C
    Par Zero dans le forum C
    Réponses: 14
    Dernier message: 06/06/2002, 09h41
  5. OmniORB : code sous Windows et Linux
    Par debug dans le forum CORBA
    Réponses: 2
    Dernier message: 30/04/2002, 17h45

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