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

Turbo Pascal Discussion :

[TP] Conversion hexadécimal à binaire


Sujet :

Turbo Pascal

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    sos
    sos est déconnecté
    Membre averti
    Inscrit en
    Décembre 2003
    Messages
    17
    Détails du profil
    Informations forums :
    Inscription : Décembre 2003
    Messages : 17
    Par défaut [TP] Conversion hexadécimal à binaire
    Bonjour tout le monde,
    je veux faire un programme qui permet de convertir un nombre hexadecimal en binaire. voici le code source:

    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
    program convert_hexa;
    uses WinCrt;
      var p:real;
      var n:string;
    function convcar(c:char):string;
    var d:integer;
    var r,e,i:integer;
    var ch1,c2:string;
     
    begin       
    if(c in['0'..'9'])then
    val(c,d,e)
    else
    d:=ord(c)-ord('A')+10;
    ch1:='0000';
    i:=4;
    repeat
    r:=d mod 2;
    str(r,c2);
    ch1[i]:=c2[1];
    i:=i-1;
    d:=d div 2;
    until (d=0);
    convcar:=ch1;
    end;  
     
     
    function convertir(ch:string):string;
    var res:string;
             i:integer;
    begin
         res:='';
         for i:=1 to length(ch)do
         begin
         res:=res+convcar(ch[i]);
         end;
         convertir:=res;
     
    end;
    function valide(ch:string):boolean;
    var i: integer;
    var b:boolean;
     
    begin
     
    b:=false;
    writeln('entrée dans valide');
    i:=0;
         repeat
         i:=i+1;
         writeln('le caractère ',i, 'est: ',ch[i]);
         b:=UPCASE(ch[i])in['0'..'9','A'..'F']  ;
         {writeln(b);}
         until ((b=false) or (i=length(ch)))
    end;
    procedure saisie(var n:string);
    begin
         repeat
         write('Donnez le nombre hexadecimal: ');
         readln(n);
         until(valide(n));
         writeln('la valeur de valide est : ',valide(n));
    end;
    begin
    saisie(n);
     
    writeln('le code binaire est: ',convertir(n));
    end.
    le problème est que la saisie accepte tout et en plus elle ne répète pas la saisie si le nombre donné n'est pas valide.
    pouvez vous m'aider s'il vous plait, je n'arrive pas à détecter l'erreur
    Merci pour votre aide

  2. #2
    Membre Expert
    Avatar de krachik
    Inscrit en
    Décembre 2004
    Messages
    1 964
    Détails du profil
    Informations forums :
    Inscription : Décembre 2004
    Messages : 1 964
    Par défaut
    Bonjour
    Juste une petite remarque sur ta fonction
    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
     
    function valide(ch:string):boolean;
    var i: integer;
    var b:boolean;
     
    begin
     
    b:=false;
    writeln('entrée dans valide');
    i:=0;
         repeat
         i:=i+1;
         writeln('le caractère ',i, 'est: ',ch[i]);
         b:=UPCASE(ch[i])in['0'..'9','A'..'F']  ;
         {writeln(b);}
         until ((b=false) or (i=length(ch)))
    end;
    Ta fonction qui supposé retourner un boolean ne le fais jamais alors je te demande pourquoi?
    quand tu sors de ta boucle quel conclusion lui retourner pour qu'il sache ta chaine est valide.Si tu as bien compris ton code il y a juste une seule instruction à mettre pour que ça marche si le reste marche bien sur
    Au passage pas la pein de declarer deux "var" un seul suffira,et indentes ton code pour qu'il soit clair @+


    [Edit] je viens de voir Résolu alors dis nous les corrections que tu as faites pour que ça marche [/edit]

  3. #3
    Membre éclairé
    Avatar de Mic**
    Homme Profil pro
    Retraité
    Inscrit en
    Avril 2007
    Messages
    57
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 77
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Avril 2007
    Messages : 57
    Billets dans le blog
    2
    Par défaut conversion binaire hexadécimale décimale sous DOS
    Bonjour,
    Voici un petit source qui fait les 3 conversions Décimal, Hexadécimal, Binaire
    sous DOS en mode texte.
    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
    USES
      Crt;
    { Email:michel.zaitri@orange.fr
       A modifier à Volonté             m & j                }
     
    TYPE
      chaine                            = STRING[16];
     
    VAR
      x,trad,bs,ch,car,ch1              : STRING;
      chb,chq,chn                       : chaine;
      nombre,nombreh,nombreb,c,d,i      : LONGINT;
      x1, x2,init,cy,color,b,ii,co      : INTEGER;
      p          : pointer;
      depart     : CHAR;
      size       : WORD;
     
    {=========================================================================}
    {affiche le cadre du module de conversion}
     
    PROCEDURE face;
     
      BEGIN
     
        TextColor(15);
        TextBackground(0);
     
        GoToXY(16,6);
        TextColor(3);
        TextBackground(0);
        Write('Conversion D‚cimale Hexad‚cimale Binaire');
        TextColor(10);
        TextBackground(0);
        GoToXY(1,7);
        Write('             ²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²');
        TextColor(2);
        TextBackground(0);
        Write('»');
        TextColor(10);
        TextBackground(0);
        GoToXY(1,8);
        Write('             ²');
        TextColor(3);
        TextBackground(0);
        Write('d');
        GoToXY(58,8);
        TextColor(10);
        TextBackground(0);
        Write('²');
        TextColor(2);
        TextBackground(0);
        Write('º');
        TextColor(10);
        TextBackground(0);
        GoToXY(1,9);
        Write('             ²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²');
        TextColor(2);
        TextBackground(0);
        Write('º');
        TextColor(10);
        TextBackground(0);
        GoToXY(1,10);
        Write('             ²');
        TextColor(3);
        TextBackground(0);
        Write('h');
        GoToXY(58,10);
        TextColor(10);
        TextBackground(0);
        Write('²');
        TextColor(2);
        TextBackground(0);
        Write('º');
        TextColor(10);
        TextBackground(0);
        GoToXY(1,11);
        Write('             ²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²');
        TextColor(2);
        TextBackground(0);
        Write('º');
        TextColor(10);
        TextBackground(0);
        GoToXY(1,12);
        Write('             ²');
        TextColor(3);
        TextBackground(0);
        Write('b');
        GoToXY(58,12);
        TextColor(10);
        TextBackground(0);
        Write('²');
        TextColor(2);
        TextBackground(0);
        Write('º');
        TextColor(10);
        TextBackground(0);
        GoToXY(1,13);
        Write('             ²²²²²²²²²²²²²²²²²²²²²²²²²²²²²By²M&J²²²²²²²²²²');
        TextColor(2);
        TextBackground(0);
        Write('º');
        GoToXY(15,14);
        Write('ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ');
        TextColor(11);
        END;
     
    {=========================================================================}
    {convertit un d‚cimal en hexa}
     
    PROCEDURE bb(q:LONGINT;
       VAR b:INTEGER;
       VAR chq:chaine);
     
      VAR
        r:LONGINT;
     
      BEGIN
        chb := '0123456789ABCDEF';
        b := 16;
        IF q > 0 THEN BEGIN
          r := q MOD b;
          q := q DIV b;
          bb (q,b,chq);
          chq := Concat (chq,Copy(chb,r+1,1)) END
        ELSE chq := '';
        END;
     
    {=========================================================================}
    {extrait x caractŠres d'une chaine}
     
    FUNCTION mid(chaine:STRING;
       deb:BYTE;
       long:BYTE):STRING;
     
      BEGIN
        mid:=Copy(chaine,deb,long);
        END;
     
    {=========================================================================}
    {convertit un d‚cimal en binaire}
     
    PROCEDURE bin(y:INTEGER;
       binair:LONGINT);
     
      VAR
        c,i       :LONGINT;
        co,ii     :BYTE;
     
      BEGIN
        c:= binair;
        FOR ii := 32 DOWNTO 1 DO BEGIN
          i:=ii;
          i :=(c*2) SHR i AND 1;
          Write (i);
          nombreb := nombreb + i;
          END;
        END;
     
    {=========================================================================}
    {convertit un binaire en d‚cimal}
     
    PROCEDURE bin_deci (ch:STRING);
     
      BEGIN
        ch1:='';
        car:='';
        i:=1;
        d:=0;
        co := Length (ch);
        FOR ii := co DOWNTO 1 DO ch1 := ch1 + mid(ch,ii,1);
        FOR ii := 1 TO co DO BEGIN
          car := mid(ch1,ii,1);
          IF car = '1' THEN BEGIN
            d:= d +i ;
            END;
          i:=i +i;
          END;
        nombre := d;
        END;
     
    {=========================================================================}
    {=========================================================================}
     
    {programme principal}
     
    BEGIN
      REPEAT
        ClrScr;
        face;
     
        GoToXY(18,8);
        Write ('CONVERTION V: 1.0 ');
     
        GoToXY(18,10);
        Write ('entrez la base de d‚part (D H ou B)');
     
        GoToXY(18,12);
        depart := ReadKey;
        IF depart = Chr(27) THEN Halt(1);
        Write ('base  '+ depart);
        Delay(500);
     
        GoToXY(18,8);
        Write('                                        ');
        GoToXY(18,10);
        Write('                                        ');
        GoToXY(18,12);
        Write('                                        ');
     
        CASE depart OF
          'h','H'  :     BEGIN
            GoToXY(18,8);
            Write ('entrez le nombre a traduire pr‚c‚d‚ de $');
     
            GoToXY(18,10);
            REPEAT
              depart := ReadKey;
              IF depart <> #13 THEN trad := trad + depart;
              Write (depart);
              UNTIL depart = #13;
            x2 := Length (trad);
            GoToXY(18,10);
            Write('                                        ');
            GoToXY(18,10);
            Write (mid(trad,2,x2));
            Val(trad,nombreh,x1);
            Str(nombreh,trad);
     
            GoToXY(18,8);
            Write('                                        ');
            GoToXY(18,8);
            Write (nombreh);
     
            GoToXY(18,12);
            bin(1,nombreh);
     
            END;
          'd','D'    :   BEGIN
            GoToXY(18,12);
            Write ('entrez le nombre a traduire');
     
            GoToXY(18,8);
            trad := ' ';
            nombre := 0;
     
            REPEAT
              depart := ReadKey;
              IF depart <> #13 THEN trad := trad + depart;
              Write (depart);
              IF depart = #13 THEN BEGIN
                bs := Copy(trad,2,Length (trad));
                trad := bs;
                END;
              UNTIL depart = #13;
            x2 := Length (trad);
            Val(trad,nombre,x1);
            Str(nombre,trad);
            IF nombre <> 0 THEN BEGIN
              GoToXY(18,10);
              Write('                                        ');
              GoToXY(18,10);
              bb(nombre,b,chn);
              Write (chn);
              END;
     
            GoToXY(18,12);
            Write('                                        ');
            GoToXY(18,12);
            bin(1,nombre);
            END;
     
     
     
          'b','B' :  BEGIN
            GoToXY(18,10);
            Write ('entrez le nombre a traduire');
     
            GoToXY(18,12);
            trad := ' ';
            nombre := 0;
     
     
            REPEAT
              depart := ReadKey;
              IF depart <> #13 THEN trad := trad + depart;
              Write (depart);
              UNTIL depart = #13;
            GoToXY(18,8);
            Write('                                        ');
            GoToXY(18,8);
            x1:=Length(trad);
            ch := mid(trad,2,x1);
            x1:=Length(ch);
            bin_deci(ch);
            IF nombre <> 0 THEN BEGIN
     
              GoToXY(18,8);
              Write('                                        ');
              GoToXY(18,8);
              Write (nombre);
     
              GoToXY(18,10);
              Write('                                        ');
              GoToXY(18,10);
              bb(nombre,b,chn);
              Write (chn);
              END;
     
     
     
            END;
          END;
        depart := ReadKey;
        nombre :=0;
        nombreh :=0;
        nombreb :=0;
        trad:='';
        ch :='';
        x1:=0;
        x2 :=0;
        UNTIL depart = Chr(27);
      END.
    A++

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

Discussions similaires

  1. Conversion de binaire à hexadécimal
    Par mlaiti dans le forum Débuter
    Réponses: 17
    Dernier message: 26/02/2011, 18h12
  2. Conversion hexadécimal en binaire
    Par mokadjo dans le forum Requêtes
    Réponses: 5
    Dernier message: 18/01/2010, 16h08
  3. conversion hexadécimal binaire
    Par naazih dans le forum Langage
    Réponses: 5
    Dernier message: 21/11/2007, 11h48
  4. conversion hexa-binaire
    Par jack69 dans le forum C
    Réponses: 8
    Dernier message: 04/04/2005, 16h33
  5. conversion ip (binaire - decimale)
    Par slim dans le forum Développement
    Réponses: 5
    Dernier message: 13/08/2004, 15h22

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