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 :

Transformer addresse mac texte en tableau d'octets


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Ingénieur
    Inscrit en
    Avril 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Ingénieur

    Informations forums :
    Inscription : Avril 2010
    Messages : 25
    Par défaut Transformer addresse mac texte en tableau d'octets
    Bonjour,

    Pourriez vous me donner la fonction exacte en C pour transformer une chaîne de caractère de taille fixe de la forme avec x un caractère hexadécimale (0-16) en un tableau de 6 octets.

    Il serait soit disant possible de le faire avec un sprintf mais je n'ai pas réussi à faire ce que je voulais.

    Actuellement je fais ça comme cela mais je ne suis pas sur que ça marche à coup sur :

    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
    	i=0;
    	u =0;
    	while(i<17) {
    		if( macTexte[i] != ':' ) {
    			pMac[u] = macTexte[i];
    			u++;
    		}
    	i++;
    	}
     
    	for(i=0; i<11; i=i+2) {
    		int a,b;
    			u = 0;			
    			if(pMac[i+u] == '0') { a = 0; };
    			if(pMac[i+u] == '2') { a = 2; };
    			if(pMac[i+u] == '3') { a = 3; };
    			if(pMac[i+u] == '4') { a = 4; };
    			if(pMac[i+u] == '5') { a = 5; };
    			if(pMac[i+u] == '6') { a = 6; };
    			if(pMac[i+u] == '7') { a = 7; };
    			if(pMac[i+u] == '8') { a = 8; };
    			if(pMac[i+u] == '9') { a = 9; };
    			if(pMac[i+u] == 'a') { a = 10; };
    			if(pMac[i+u] == 'b') { a = 11; };
    			if(pMac[i+u] == 'c') { a = 12; };
    			if(pMac[i+u] == 'd') { a = 13; };
    			if(pMac[i+u] == 'e') { a = 14; };
    			if(pMac[i+u] == 'f') { a = 15; };
    			u = 1;
    			if(pMac[i+u] == '0') { b = 0; };
    			if(pMac[i+u] == '1') { b = 1; };
    			if(pMac[i+u] == '2') { b = 2; };
    			if(pMac[i+u] == '3') { b = 3; };
    			if(pMac[i+u] == '4') { b = 4; };
    			if(pMac[i+u] == '5') { b = 5; };
    			if(pMac[i+u] == '6') { b = 6; };
    			if(pMac[i+u] == '7') { b = 7; };
    			if(pMac[i+u] == '8') { b = 8; };
    			if(pMac[i+u] == '9') { b = 9; };
    			if(pMac[i+u] == 'a') { b = 10; };
    			if(pMac[i+u] == 'b') { b = 11; };
    			if(pMac[i+u] == 'c') { b = 12; };
    			if(pMac[i+u] == 'd') { b = 13; };
    			if(pMac[i+u] == 'e') { b = 14; };
    			if(pMac[i+u] == 'f') { b = 15; };
    		mac[i/2] = (u_char) a*16 + b;

  2. #2
    Membre Expert
    Profil pro
    Inscrit en
    Août 2006
    Messages
    1 104
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 1 104
    Par défaut
    Salut,

    Tu peux faire quelque chose comme ça :

    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
    #include <stdio.h>
     
    int main (void)
    {
    	unsigned int tableau[6] ;
    	char chaine[256] = "01:23:45:67:89:ab" ;
    	int n ;
     
    	if ( sscanf ( chaine , "%x:%x:%x:%x:%x:%x" , &tableau [ 0 ] , &tableau [ 1 ] , &tableau [ 2 ] , &tableau [ 3 ] , &tableau [ 4 ] , &tableau [ 5 ] ) == 6 )
    	{
    		printf ( "%s : \n\n" , chaine ) ;
    		for ( n = 0 ; n < 6 ; n++ )
    		{
    			printf ( "nombre #%d : %d (%02x hexa)\n" , n+1 , tableau [ n ] ,  tableau [ n ] ) ;
    		}
    	} else
    		printf ( "Erreur dans la chaine\n");
     
    	return 0;
    }

  3. #3
    Membre averti
    Homme Profil pro
    Ingénieur
    Inscrit en
    Avril 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Ingénieur

    Informations forums :
    Inscription : Avril 2010
    Messages : 25
    Par défaut
    Salut,

    Merci de me répondre mais la seconde partie de ton code n'est pas du tout ce que je cherche à faire.

    Je veux récupérer 6 octets (u_char) soit 48 bits. Et pas afficher 6 entiers (int) de 32 bits.

  4. #4
    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
    Alors, transforme les en unsigned char. Pour continuer sur la proposition de jeroman :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #include <stdio.h>
     
    int main (void)
    {
       unsigned int tableau[6] ;
       unsigned char mac[6] ;
       char chaine[256] = "01:23:45:67:89:ab" ;
       int n ;
       if ( sscanf ( chaine , "%x:%x:%x:%x:%x:%x" , &tableau [ 0 ] , &tableau [ 1 ] , &tableau [ 2 ] , &tableau [ 3 ] , &tableau [ 4 ] , &tableau [ 5 ] ) == 6 )
           for ( n = 0 ; n < 6 ; n++ ) mac[n] = tableau[n];
       else printf ( "Erreur dans la chaine\n");	
       return 0;
    }

Discussions similaires

  1. [WD-MAC 2011] Transformer texte en tableau
    Par Marco777 dans le forum Word
    Réponses: 3
    Dernier message: 27/04/2013, 01h37
  2. [MySQL] transformer full text en tableau
    Par gtraxx dans le forum PHP & Base de données
    Réponses: 2
    Dernier message: 06/08/2009, 11h38
  3. Réponses: 6
    Dernier message: 21/05/2008, 10h50
  4. Conversion d'une chaîne en tableau d'octets
    Par marsupilami34 dans le forum Langage
    Réponses: 11
    Dernier message: 22/06/2005, 14h44
  5. [Kylix] stockage d'un tableau d'octets dans interbase
    Par georges1001 dans le forum EDI
    Réponses: 1
    Dernier message: 16/09/2003, 14h14

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