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 :

reinterpret_cast pour sérialiser en char des types primitifs


Sujet :

C++

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2012
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Bâtiment

    Informations forums :
    Inscription : Août 2012
    Messages : 7
    Points : 6
    Points
    6
    Par défaut reinterpret_cast pour sérialiser en char des types primitifs
    bonjour ,

    je cherche une fonction qui prend en parametre un type primitif , et qui retourne un pointeur sur un tableau de unsigned char contenant la valeur du parametre.

    l'objectif est de faire une librairie pour arduino ( pour communiquer avec node.js ).

    je suis arrivée pour les "int" , mais le même procédé ne fonctionne pas avec les "float" :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    unsigned char * tab ;
    float test = 256 ;
     
    tab = *( reiterpret_cast<*unsigned char>( $test ) );
    tab[0] = 01 et tab[1] = 00 ( LE ).
    le résultat est bon mais avec float ça marche pas.

    avez vous une solution meilleur ?

  2. #2
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 376
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 376
    Points : 41 545
    Points
    41 545
    Par défaut
    Ceci devrait marcher:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    tab = reinterpret_cast<unsigned char*>( &test );
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  3. #3
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 376
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 376
    Points : 41 545
    Points
    41 545
    Par défaut
    Normalement, (float)256 correspond à 0x43800000, ou en binaire:
    0b01000011 10000000 00000000 00000000
    (J'ai mis l'exposant en rouge: Il vaut 8+127).
    Tout ça correspond à +1.000000 * 2^8.
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  4. #4
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2012
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Bâtiment

    Informations forums :
    Inscription : Août 2012
    Messages : 7
    Points : 6
    Points
    6
    Par défaut
    merci Médinoc ,

    ça fonctionne, code arduino :

    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
     
     
    unsigned char * tab ;
    float test = 256 ;
     
    // the setup routine runs once when you press reset:
    void setup() {     
     
    tab = reinterpret_cast<unsigned char*>( &test ); 
    Serial.begin(9600);
     
    Serial.println( test );
    Serial.println( tab[0] );
    Serial.println( tab[1] );
     
    test = 256.1;
    tab = reinterpret_cast<unsigned char*>( &test ); 
    Serial.println( test );
    Serial.println( tab[0] );
    Serial.println( tab[1] );
     
    test = -0.1;
    tab = reinterpret_cast<unsigned char*>( &test ); 
    Serial.println( test );
    Serial.println( tab[0] );
    Serial.println( tab[1] );
     
    test = 3333.1;
    tab = reinterpret_cast<unsigned char*>( &test ); 
    Serial.println( test );
    Serial.println( tab[0] );
    Serial.println( tab[1] );
     
    Serial.println( test );
    Serial.println( tab[0] );
    Serial.println( tab[1] );
     
    /////////////////////
    int test = 256 ;
    tab = reinterpret_cast<unsigned char*>( &test ); 
    Serial.println( String( "\nint " ) + test );
    Serial.println( tab[0] );
    Serial.println( tab[1] );
     
    unsigned int test1 = 256 ;
    tab = reinterpret_cast<unsigned char*>( &test1 ); 
    Serial.println( String( "\nuInt " ) + test );
    Serial.println( tab[0] );
    Serial.println( tab[1] );
     
    char test2 = 97 ;
    tab = reinterpret_cast<unsigned char*>( &test2 ); 
    Serial.println( String( "\nchar " ) + test2 );
    Serial.println( tab[0] );
    Serial.println( tab[1] );
     
    unsigned char test3 = 97 ;
    tab = reinterpret_cast<unsigned char*>( &test3 ); 
    Serial.println( String( "\nuChar " ) + test3 );
    Serial.println( tab[0] );
    Serial.println( tab[1] );
     
    }
     
    // the loop routine runs over and over again forever:
    void loop() {
     
    }
    résultat :

    256.00
    0
    0
    256.10
    205
    12
    -0.10
    205
    204
    3333.10
    154
    81
    3333.10
    154
    81

    int 256
    0
    1

    uInt 256
    0
    1

    char a
    97
    97

    uChar 97
    97
    0


    comment je peut faire l opération inverse ? retrouver un type premier a partir d 'un pointeur sur tableau unsigned char ?

    j'utilise les "unsigned char" parce que ça va jusqu a 256 , mais est ce que ça ferait une différence avec "char" ?

  5. #5
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 376
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 376
    Points : 41 545
    Points
    41 545
    Par défaut
    Facile:
    Code C++ : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    unsigned char test[4];
    /*Ici j'initialise manuellement parce que c'est juste un exemple*/
    test[0] = 0x43;
    test[1] = 0x80;
    test[2] = 0x00;
    test[3] = 0x00;
    float f = *reinterpret_cast<float*>(test);

    Note que tu vas te heurter à des problèmes d'endianness si les autres machines ne sont pas des x86. Normalement quand on sérialise, on force également une endianness (typiquement big-endian, la convention "network").
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

Discussions similaires

  1. Passer des types primitifs par référence constante ?
    Par FluidBlow dans le forum Langage
    Réponses: 2
    Dernier message: 24/12/2010, 15h43
  2. [Free Pascal] Suppression des messages hint pour des types File
    Par bubulemaster dans le forum Free Pascal
    Réponses: 5
    Dernier message: 03/09/2008, 21h18
  3. L'equivalent d'un ArrayList pour types primitif?
    Par javanoiid dans le forum Collection et Stream
    Réponses: 6
    Dernier message: 04/03/2008, 17h15
  4. Mysql : choix des types pour les champs entre :
    Par Thierry8 dans le forum Administration
    Réponses: 3
    Dernier message: 14/06/2006, 08h22
  5. Extension des Types de Données(CHAR,CLOB)
    Par blackstreet dans le forum Oracle
    Réponses: 9
    Dernier message: 28/04/2006, 22h39

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