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 :

Convertir char* en string


Sujet :

C++

  1. #1
    Candidat au Club
    Inscrit en
    Juin 2004
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Juin 2004
    Messages : 3
    Points : 2
    Points
    2
    Par défaut Convertir char* en string
    Bonjour,

    c'est une question très bête mais il m'est impossible de copier le contenu d'un char* dans un string. Voir à la fin du code, je n'arrive pas à copier le Byte* (c'est un unsigned char) dans mon string machin.

    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
     
    Byte*		ATTR_payload;
    	string		truc;
    	string		machin;
     
    	int i = 0;
     
    	int nbRecords = 0;
    	map <string, string> mapREC;
     
     
    	for (rd_ptr = DB_POS_DB_REC; rd_ptr < db[dbNum].header.file_end; rd_ptr += sizeof (DbRecHdrType) + hdr.size)
    	{
    ...
     
    	if  (read(db[dbNum].file_desc, (char *)ATTR_payload, hdr.size ) ) { 				// reading the payload
    			printf("payload : ");
    			for (i = 0 ; i < hdr.size -1 ; i++)
    				printf("%0X", ATTR_payload[i]);
    			printf("\n");
     
    		} else
    			return HandleDBOpenError(dbNum, DB_RecordReadFailed, TRUE);
     
    		strstream glop;				// conversion int to string dégueulasse
    		glop << ATTR_tmp;
    		glop >> truc;
    		cout << "truc = " << truc << endl;
     
    		machin.c_str() = ATTR_payload;
     
    		nbRecords++;
     
    	}
    Des pistes, je suis preneur

  2. #2
    Membre expérimenté
    Avatar de Patriarch24
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2003
    Messages
    1 047
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2003
    Messages : 1 047
    Points : 1 640
    Points
    1 640
    Par défaut
    machin.c_str() = ATTR_payload;
    c_str() est une fonction qui renvoie un const char* (le contenu de la chaîne).
    Pourquoi faire compliqué quand c'est si simple ?

    ps : je pense qu'en lisant les insultes du compilo, tu aurais compris

  3. #3
    Candidat au Club
    Inscrit en
    Juin 2004
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Juin 2004
    Messages : 3
    Points : 2
    Points
    2
    Par défaut
    Merci de me répondre si rapidement.

    J'ai essayé aussi

    Le résultat

    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
     
    /local/armeunie/Projet/mg_xml/util/src/xml_util.cpp: In function `Status
       GetRecords(DbType*, int)':
    /local/armeunie/Projet/mg_xml/util/src/xml_util.cpp:192: error: ambiguous
       overload for 'operator=' in 'machin = ATTR_payload'
    /agl/tools/xgcc_montavista/v5/cge3.1/usr/include/c++/3.3.1/bits/basic_string.h:358: error: candidates
       are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT,
       _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits,
       _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc =
       std::allocator<char>] <near match>
    /agl/tools/xgcc_montavista/v5/cge3.1/usr/include/c++/3.3.1/bits/basic_string.h:361: error:
                      std::basic_string<_CharT, _Traits, _Alloc>&
       std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with
       _CharT = char, _Traits = std::char_traits<char>, _Alloc =
       std::allocator<char>] <near match>
    /agl/tools/xgcc_montavista/v5/cge3.1/usr/include/c++/3.3.1/bits/basic_string.h:364: error:
                      std::basic_string<_CharT, _Traits, _Alloc>&
       std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT =
       char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>] <near
       match>
    *** Error code 1
    clearmake: Error: Build script failed for "../obj/xml_util.o"
     
    clearmake[1]: Leaving directory `/local/armeunie/Projet/mg_xml/util/build'
    *** Error code 1
    clearmake: Error: Build script failed for "subdirs"
    Je me tape ça
    error: ambiguous overload for 'operator=' in 'machin = ATTR_payload'


  4. #4
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    Le problème c'est que std::string stocke des char, et toi tu veux lui passer des unsigned char. Il faut donc savoir si :
    - tu veux en fait un std::string de unsigned char
    - tu veux convertir tes unsigned char en char

  5. #5
    Membre expérimenté
    Avatar de Patriarch24
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2003
    Messages
    1 047
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2003
    Messages : 1 047
    Points : 1 640
    Points
    1 640
    Par défaut
    Le problème c'est que std::string stocke des char, et toi tu veux lui passer des unsigned char. Il faut donc savoir si :
    - tu veux en fait un std::string de unsigned char
    - tu veux convertir tes unsigned char en char
    Ou alors utiliser un basic_string<unsigned char> (avec un typedef bien sûr ) ?

  6. #6
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    Citation Envoyé par Patriarch24
    Ou alors utiliser un basic_string<unsigned char> (avec un typedef bien sûr ) ?
    C'est ce qu'il fallait comprendre avec
    tu veux en fait un std::string de unsigned char
    Mais dans des termes plus accessibles aux débutants.

Discussions similaires

  1. Convertir char* en string sous Linux ?
    Par Zangdaarr dans le forum Langage
    Réponses: 8
    Dernier message: 25/03/2011, 15h00
  2. Convertir char ou String en Int
    Par Invité(e) dans le forum ASP
    Réponses: 2
    Dernier message: 10/06/2008, 16h17
  3. Convertir Char* en String
    Par Blunt dans le forum C++/CLI
    Réponses: 1
    Dernier message: 01/07/2007, 16h46
  4. convertir un char * en string
    Par Seij dans le forum C++
    Réponses: 5
    Dernier message: 08/03/2006, 22h23
  5. Réponses: 12
    Dernier message: 31/01/2006, 21h46

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