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 Discussion :

passage de variable type 128 bits conversion vers 2 _int64


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Developer
    Inscrit en
    Juin 2004
    Messages
    194
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations professionnelles :
    Activité : Developer

    Informations forums :
    Inscription : Juin 2004
    Messages : 194
    Par défaut passage de variable type 128 bits conversion vers 2 _int64
    Bonjour,

    Il me faut une fonction dans laquelle il faut pouvoir remplir un tableau d'entiers de 16 bytes (128 bits) à partir d'un passage de parametre dans une fonction call. Comme malheurerusement il n'existe
    pas de type de variable superiaur à _int64 (64bits), ici sans doute qu'il faut faire un genre de strtol(,,) afin de spliter l'entrée saisie vers 2 _int64 je pense. J'ai déjà commencé par exemple cette fonction asciitobinary(x,x).

    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
     
        ascii2binary (char* sourceAscii, char* destBin)
        {
     
            int64 lowPart = 0;
            int highPart = 0;
     
            for(int i=0; i<20; ++i)
            {
                lowPart += (sourceAscii[i]-48)*exp(10,i); //exemple= 10 exp 0                               //correspond au premiere chiffre
                                      //de 654342, càd à 2.
     
                                      // 48 pour enlever le 0 du ascii
     
            }
            for(int i=20; i<38; ++i)
            {
                highPart += (sourceAscii[i]-48)*exp(10,i);
     
            }
     
     
            // ici conversion des 2 _int64 vers les destBin[] avec des decalages et   des   //masquages
     
        }
    Pourriez vous m'aider à faire la conversion des 2 _int64 vers les destBin[] ?


    Merci .

  2. #2
    Membre confirmé
    Profil pro
    Developer
    Inscrit en
    Juin 2004
    Messages
    194
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations professionnelles :
    Activité : Developer

    Informations forums :
    Inscription : Juin 2004
    Messages : 194
    Par défaut
    bonjour,

    voila j'ai fait mon programme il reste toutefois un petit soucis.

    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
     
     
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
     
    void ascii2binary (char* sourceAscii, char* destbin, int size)
    {
     
    __int64 lowPart = 0; 
    __int64 highPart = 0;
     
    for(int i=0; i<20; ++i)
    {
               lowPart += (sourceAscii[i]-48)*exp(i); //exemple= 10 exp 0 
                                       //correspond au premier chiffre dans
                                       //654342 c'est le 2.
                                       //48 pour enlever le 0 du ascii
    }
     
    for(int j=20; j<38; ++j)
    {
            highPart += (sourceAscii[j]-48)*exp(j);
     
    }
     
    printf("value of lowPart: %ld\n",lowPart);
    printf("value of highPart: %ld\n",highPart);
    printf("sizeof lowpart & higpart is : %d\n", sizeof(lowPart));
     
    destbin[0] =(char)(lowPart & 0x00000000000000FF);
    destbin[1] =(char)((lowPart>>8) & 0x000000000000FF);
    destbin[2] =(char)((lowPart>>16) & 0x0000000000FF);
    destbin[3] =(char)((lowPart>>24) & 0x00000000FF);
    destbin[4] =(char)((lowPart>>32) & 0x000000FF);
    destbin[5] =(char)((lowPart>>40) & 0x0000FF);
    destbin[6] =(char)((lowPart>>48) & 0x00FF);
    destbin[7] =(char)((lowPart>>56) & 0xFF);
     
    destbin[8] =(char)(highPart & 0x00000000000000FF);
    destbin[9] =(char)((highPart>>8) & 0x000000000000FF);
    destbin[10]=(char)((highPart>>16) & 0x0000000000FF);
    destbin[11]=(char)((highPart>>24) & 0x00000000FF);
    destbin[12]=(char)((highPart>>32) & 0x000000FF);
    destbin[13]=(char)((highPart>>40) & 0x0000FF);
    destbin[14]=(char)((highPart>>48) & 0x00FF);
    destbin[15]=(char)((highPart>>56) & 0xFF);
     
     
    for( int k =0; k<size ; ++k)
    {
    printf("string destbin is: %c\n", destbin[k]); //bug
    }
     
    }
     
     
    void main (int args, char * argv[])
    {
     
    int indice;
     
    char *tab[20];
    char* ptabdest;
     
    // char* ptab;
    // ptab = (char*)malloc(16); //
    // memset (ptab,0,16);
     
    if(args != 2){
    printf("erreur\n");
    printf("usage: call program + <numbers>");
    exit(1);
    }
     
    indice = 0;
    while(argv[1][indice] != '\0'){
    indice++;
    }
     
    ptabdest = (char*)malloc(indice);
    printf("le string %s contient %d lettres\n", argv[1], indice);
     
    tab[0]=argv[1];
    printf("passage a la variable char *tab[] contient:%s\n", argv[1]);
     
    ascii2binary( tab[0], ptabdest, indice);
     
    system("PAUSE");
     
    }
    Si je passe en parametre par exemple : 123456789123456789 ,
    je ne retrouve pas dans destbin[] ces données là ?
    Qu'est ce qui ne va pas ?

    Merci

  3. #3
    Expert confirmé
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
               lowPart += (sourceAscii[i]-48)*exp(i); //exemple= 10 exp 0 
    ...
            highPart += (sourceAscii[j]-48)*exp(j);
    Que vient faire dans ce code la fonction exponentielle ???

  4. #4
    Membre confirmé
    Profil pro
    Developer
    Inscrit en
    Juin 2004
    Messages
    194
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations professionnelles :
    Activité : Developer

    Informations forums :
    Inscription : Juin 2004
    Messages : 194
    Par défaut
    Je pensais agir sur les digits un peu comme j'ai mis dans mon commentaire l'équivalent aux masques des operations binaires mais pour des decimaux
    apparement c'est pas la bonne méthode. Que pourrais je faire?

    Les paramètres que je passe est ce que c'est bon?

    Merci

  5. #5
    Expert confirmé
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Par défaut
    Si je comprend ton problème, la solution de passer par des entiers 64 bits pour former ton tableau d'octets n'est pas bonne : tu ne peux pas calculer de cette façon la partie de forts poids et celle des faibles poids.

    Je te propose plutôt l'approche suivante :
    - l'octet de poids faible est le reste de la division du nombre par 256.
    - l'octet suivant est obtenu en prenant le quotient de la division précédente et en en prenant le reste de la division par 256
    - et ainsi de suite.

    Ton problème est que le nombre à diviser est codé en décimal et non pas en binaire (sinon tu n'aurais pas de problèmes évidemment)
    Il te faut donc coder la division d'un nombre décimal par 256.
    Par exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    unsigned int div256(unsigned char tab[], unsigned int nbtab)
    {
     unsigned int A, R ,i;
     A = tab[0];
     R = 0;
     for(i = 0; i<nbtab;i++)
     {
       A = 10*R +tab[i];
       tab[i] = A/256;
       R = A%256;
     }
     return R;
    }
    Ici tab contient la suite des chiffres décimaux, un chiffre par élément du tableau, le poids fort en position 0. nbtab est le nombre d'éléments de ce tableau. La fonction renvoie le reste sous forme binaire et place le quotient dans tab sous forme décimale (tab est donc modifié).

    Il ne reste plus qu'à placer les restes obtenus dans le tableau de destination.
    Par exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    void ascii2binary (unsigned char tab[], unsigned int nbtab, unsigned char dest[],
       unsigned int nbdest)
    {
      unsigned int i;
      // Si les données de départ sont en caractères :
      for(i=0 ; i<nbtab ;i++ ) tab[i] = tab[i]-'0';
      for(i=0 ; i<nbdest;i++ ) dest[i] = div256(tab,nbtab);
    }
    tab a le même format que précédemment. Le résultat est placé dans dest, tableau de nbdest éléments, avec dans dest[0] les poids faibles.
    On peut utiliser tout ça de la façon suivante

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
      unsigned char dest[15];
      unsigned char tab[] = {'1','2','3','4','5','6','7','8','9'};
    ...
      int lentab = sizeof tab /sizeof *tab;
      int lendest = sizeof dest /sizeof *dest;
      Convert(tab,lentab,dest,lendest);

  6. #6
    Membre confirmé
    Profil pro
    Developer
    Inscrit en
    Juin 2004
    Messages
    194
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations professionnelles :
    Activité : Developer

    Informations forums :
    Inscription : Juin 2004
    Messages : 194
    Par défaut
    Merci diogene,

    voila j'ai adapter mon programme mais
    je n'ai pas encore le résultat attendu dans la vérification?

    Deplus je ne comprend pas le type de retour de la fonction div256 est un entier ?

    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
     
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
     
    unsigned int div256(unsigned char tab[], unsigned int nbtab)
    {
     unsigned int A, R ,i;
     A = tab[0];
     R = 0;
     for(i = 0; i<nbtab;i++)
     {
       A = 10*R +tab[i];
       tab[i] = A/256;
       R = A%256;
     }
     return R;
    }
     
    void ascii2binary (unsigned char tab[], unsigned int nbtab, unsigned char dest[],
       unsigned int nbdest)
    {
      unsigned int i;
      // Si les données de départ sont en caractères :
      for(i=0 ; i<nbtab ;i++ ) tab[i] = tab[i]-'0';
      for(i=0 ; i<nbdest;i++ ) dest[i] = div256(tab,nbtab);
    }
     
     
    void main (int args, char * argv[])
    {
     
      int indice;
      unsigned char tab[40];
      unsigned char dest[40];
     
     
      if(args != 2)
      {
    	  printf("erreur\n");
    	  printf("usage: call program with string in params");
          exit(1);
      }
     
      indice = 0;
      while(argv[1][indice] != '\0')
      {
          indice++;
      }
     
      printf("le parametre %s contient %d lettres\n", argv[1], indice);
     
      for(int c=0; c<indice;++c)
      {
    	  tab[c]=(char)argv[1+c];
      }
     
      printf("tab[] contient:%s\n", argv[1]);
     
     
      int lentab = sizeof tab /sizeof *tab;
      int lendest = sizeof dest /sizeof *dest;
     
      ascii2binary(tab,lentab,dest,lendest);
     
      printf(" verification : \n");
     
      for (int i=0; i<lentab;++i)
    	  printf("apres conversion tab[] contient:%c\n", tab[i]);
     
      for (int j=0; j<lendest;++j)
          printf("apres conversion dest[] contient:%c\n", dest[j]);
     
     
      system("PAUSE");
     
    }
    la capture donne :
    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
     
     
    le parametre 123456789123456789123456789123456789 contient 36 lettres
    tab[] contient:123456789123456789123456789123456789
     verification :
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion tab[] contient:
    apres conversion dest[] contient:4
    apres conversion dest[] contient:X
    apres conversion dest[] contient:↑
    apres conversion dest[] contient:R
    apres conversion dest[] contient:.
    apres conversion dest[] contient:p
    apres conversion dest[] contient:Ó
    apres conversion dest[] contient:u
    apres conversion dest[] contient:É
    apres conversion dest[] contient:┬
    apres conversion dest[] contient:─
    apres conversion dest[] contient:)
    apres conversion dest[] contient:▄
    apres conversion dest[] contient:╬
    apres conversion dest[] contient:g
    apres conversion dest[] contient:\
    apres conversion dest[] contient:ç
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    apres conversion dest[] contient:
    Appuyez sur une touche pour continuer...
    merci.

Discussions similaires

  1. Réponses: 6
    Dernier message: 15/03/2010, 10h59
  2. Réponses: 18
    Dernier message: 10/04/2009, 10h27
  3. Réponses: 4
    Dernier message: 22/01/2007, 18h12
  4. Passage de variables de DELPHI vers RAVE
    Par e120650 dans le forum Bases de données
    Réponses: 2
    Dernier message: 08/02/2005, 12h17
  5. déclaration de variables de 16, 32 et 128 bits
    Par samipate dans le forum C++
    Réponses: 10
    Dernier message: 30/12/2004, 22h33

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