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

Langage Perl Discussion :

conversion nombre binaire -> decimal


Sujet :

Langage Perl

  1. #1
    Membre à l'essai
    Inscrit en
    Février 2004
    Messages
    25
    Détails du profil
    Informations forums :
    Inscription : Février 2004
    Messages : 25
    Points : 22
    Points
    22
    Par défaut conversion nombre binaire -> decimal
    Bonjour

    j'ai un nombre que je convertie en binaire
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $binaire=sprintf("%08b",$decimal);
    je fais plusiers calculs et j'obtiens un nouveau nombre binaire.

    je voudrais convertir ce nouveau nombre en decimal mais je n'y arrive pas.

    J'espere que l'un d'entre vous a une solution.

    Merci

  2. #2
    Membre à l'essai
    Inscrit en
    Février 2004
    Messages
    25
    Détails du profil
    Informations forums :
    Inscription : Février 2004
    Messages : 25
    Points : 22
    Points
    22
    Par défaut
    finalement j'ai réussi donc je le poste au cas ou cela interesserait des gens

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    @ch_bin=split("",$binaire);     #transforme la chaine binaire en tableau
    $decimal = 0;
    for ($i=0;$i<length($binaire);$i++)
    {
        $decimal = 2*$decimal + $ch_bin[$i];
    }

  3. #3
    tfe
    tfe est déconnecté
    Membre régulier
    Profil pro
    Inscrit en
    Novembre 2005
    Messages
    85
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2005
    Messages : 85
    Points : 95
    Points
    95
    Par défaut
    pack/unpack sont tes amis

  4. #4
    Membre à l'essai
    Inscrit en
    Février 2004
    Messages
    25
    Détails du profil
    Informations forums :
    Inscription : Février 2004
    Messages : 25
    Points : 22
    Points
    22
    Par défaut
    Citation Envoyé par tfe
    pack/unpack sont tes amis
    je n'ai jamais réussi à les utiliser
    ca me renvoyai des caractères etranges

  5. #5
    Expert éminent
    Avatar de Jedai
    Homme Profil pro
    Enseignant
    Inscrit en
    Avril 2003
    Messages
    6 245
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Avril 2003
    Messages : 6 245
    Points : 8 586
    Points
    8 586
    Par défaut
    Il me semble pourtant que cette section de perlpacktut est explicite :
    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
     
    Bit Strings
     
    Bits are the atoms in the memory world. Access to individual bits may have to be used either as a last resort or because it is the most convenient way to handle your data. Bit string (un)packing converts between strings containing a series of 0 and 1 characters and a sequence of bytes each containing a group of 8 bits. This is almost as simple as it sounds, except that there are two ways the contents of a byte may be written as a bit string. Let's have a look at an annotated byte:
     
         7 6 5 4 3 2 1 0
       +-----------------+
       | 1 0 0 0 1 1 0 0 |
       +-----------------+
        MSB           LSB
     
    It's egg-eating all over again: Some think that as a bit string this should be written ``10001100'' i.e. beginning with the most significant bit, others insist on ``00110001''. Well, Perl isn't biased, so that's why we have two bit string codes:
     
       $byte = pack( 'B8', '10001100' ); # start with MSB
       $byte = pack( 'b8', '00110001' ); # start with LSB
     
    It is not possible to pack or unpack bit fields - just integral bytes. pack always starts at the next byte boundary and ``rounds up'' to the next multiple of 8 by adding zero bits as required. (If you do want bit fields, there is vec in the perlfunc manpage. Or you could implement bit field handling at the character string level, using split, substr, and concatenation on unpacked bit strings.)
     
    To illustrate unpacking for bit strings, we'll decompose a simple status register (a ``-'' stands for a ``reserved'' bit):
     
       +-----------------+-----------------+
       | S Z - A - P - C | - - - - O D I T |
       +-----------------+-----------------+
        MSB           LSB MSB           LSB
     
    Converting these two bytes to a string can be done with the unpack template 'b16'. To obtain the individual bit values from the bit string we use split with the ``empty'' separator pattern which dissects into individual characters. Bit values from the ``reserved'' positions are simply assigned to undef, a convenient notation for ``I don't care where this goes''.
     
       ($carry, undef, $parity, undef, $auxcarry, undef, $zero, $sign,
        $trace, $interrupt, $direction, $overflow) =
          split( //, unpack( 'b16', $status ) );
     
    We could have used an unpack template 'b12' just as well, since the last 4 bits can be ignored anyway.
    As-tu essayé cette méthode ?

    --
    Jedaï

  6. #6
    Membre à l'essai
    Inscrit en
    Février 2004
    Messages
    25
    Détails du profil
    Informations forums :
    Inscription : Février 2004
    Messages : 25
    Points : 22
    Points
    22
    Par défaut
    désolé je parle pas anglais

  7. #7
    Membre expert
    Avatar de 2Eurocents
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    2 177
    Détails du profil
    Informations personnelles :
    Âge : 54
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 2 177
    Points : 3 166
    Points
    3 166
    Par défaut
    Alors essaye ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    my $a = "01000010";
    my $b = pack ("B8", $a);
    my $c = pack ("C", $b);
    my $d = pack ("C", $c);
    my $e = unpack ("B8", $d);
     
    print "chaine binaire $a\n";
    print "conversion caractère $b\n";
    print "evaluation numérique ".ord($b)."\n";
    print "conversion numérique $c\n";
    print "reconversion caractère $d\n";
    print "reconversion chaine binaire $e\n";
    Si la langue du perlpacktut que t'a donné jedai te gène (il me semble que sa traduction en français n'est pas achevée), tu peux regarder la doc de pack et unpack dans perlfunc. Elle est très intéressante, mais il faut quand même pratiquer à côté pour tout appréhender .
    La FAQ Perl est par ici
    : La fonction "Rechercher", on aurait dû la nommer "Retrouver" - essayez et vous verrez pourquoi !

  8. #8
    Expert éminent
    Avatar de Jedai
    Homme Profil pro
    Enseignant
    Inscrit en
    Avril 2003
    Messages
    6 245
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Avril 2003
    Messages : 6 245
    Points : 8 586
    Points
    8 586
    Par défaut
    Et je te conseille très fortement d'apprendre l'anglais, qui malheureusement est tout à fait indispensable pour un informaticien.

    --
    Jedaï

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

Discussions similaires

  1. conversion nombre binaire en decimal
    Par mendezino dans le forum Windows Forms
    Réponses: 5
    Dernier message: 20/04/2015, 20h03
  2. Extraction de nombre binaire et conversion
    Par christophe_fr dans le forum Scilab
    Réponses: 0
    Dernier message: 25/03/2009, 13h37
  3. Conversion d'un nombre binaire en décimal
    Par grungy-soul dans le forum Général Java
    Réponses: 7
    Dernier message: 28/05/2008, 09h23
  4. Réponses: 7
    Dernier message: 10/05/2007, 16h24
  5. [LG] Convertir un nombre binaire en décimal
    Par minela28x dans le forum Langage
    Réponses: 5
    Dernier message: 05/01/2006, 10h33

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