7 réponses
Bon alors je me suis renseigné auprès de mon professeur et il m'a répondu qu'il y avait au moins deux solutions, à peu près aussi rapides. Ces deux solutions sont :
string Str = "toto";
const char *CstStr1 = Str.data ();
const char *CstStr2 = Str.c_str ();
Je n'ai pas encore eu le temps de tester mais si vous voulez vous pourrez le faire vous-même, en choisissant une de ses 2 lignes et en la mettant comme corps de la fonction StrToChar (voir source plus haut).
Merci à tous pour votre aide, à bientôt peut-être.
string Str = "toto";
const char *CstStr1 = Str.data ();
const char *CstStr2 = Str.c_str ();
Je n'ai pas encore eu le temps de tester mais si vous voulez vous pourrez le faire vous-même, en choisissant une de ses 2 lignes et en la mettant comme corps de la fonction StrToChar (voir source plus haut).
Merci à tous pour votre aide, à bientôt peut-être.
Salut Tion,
c'est de string.h que j'ai besoin pour t'aider
car si par exemple on a la classe string suivante dans string.h
class string{
private:
char* s;
}
le cast le plus rapide sera
string s = "toto";
const char* s2 = (const char*) ((char*)&s) ;
c'est de string.h que j'ai besoin pour t'aider
car si par exemple on a la classe string suivante dans string.h
class string{
private:
char* s;
}
le cast le plus rapide sera
string s = "toto";
const char* s2 = (const char*) ((char*)&s) ;
bijad2010
Messages postés
40
Date d'inscription
jeudi 22 avril 2010
Statut
Membre
Dernière intervention
2 décembre 2015
26 avril 2010 à 12:00
26 avril 2010 à 12:00
SLT,
static void initName()
{
if(nPkt_ >= PT_NTYPE+1)
return;
char **nameNew = new char*[PT_NTYPE+1];
for(unsigned int i = (unsigned int)PT_SMAC+1; i < nPkt_; i++)
{
nameNew[i] = name_[i];
}
if(!nPkt_)
delete [] name_;
name_ = nameNew;
nPkt_ = PT_NTYPE+1;
name_[PT_TCP]= "tcp";
name_[PT_UDP]= "udp";
name_[PT_CBR]= "cbr";
name_[PT_AUDIO]= "audio";
name_[PT_VIDEO]= "video";
name_[PT_ACK]= "ack";
name_[PT_START]= "start";
name_[PT_STOP]= "stop";
name_[PT_PRUNE]= "prune";
name_[PT_GRAFT]= "graft";
name_[PT_GRAFTACK]= "graftAck";
name_[PT_JOIN]= "join";
name_[PT_ASSERT]= "assert";
name_[PT_MESSAGE]= "message";
name_[PT_RTCP]= "rtcp";
name_[PT_RTP]= "rtp";
name_[PT_RTPROTO_DV]= "rtProtoDV";
name_[PT_CtrMcast_Encap]= "CtrMcast_Encap";
name_[PT_CtrMcast_Decap]= "CtrMcast_Decap";
name_[PT_SRM]= "SRM";
name_[PT_REQUEST]= "sa_req";
name_[PT_ACCEPT]= "sa_accept";
name_[PT_CONFIRM]= "sa_conf";
name_[PT_TEARDOWN]= "sa_teardown";
name_[PT_LIVE]= "live";
name_[PT_REJECT]= "sa_reject";
name_[PT_TELNET]= "telnet";
name_[PT_FTP]= "ftp";
name_[PT_PARETO]= "pareto";
name_[PT_EXP]= "exp";
name_[PT_INVAL]= "httpInval";
name_[PT_HTTP]= "http";
name_[PT_ENCAPSULATED]= "encap";
name_[PT_MFTP]= "mftp";
name_[PT_ARP]= "ARP";
name_[PT_MAC]= "MAC";
name_[PT_TORA]= "TORA";
name_[PT_DSR]= "DSR";
name_[PT_AODV]= "AODV";
name_[PT_IMEP]= "IMEP";
name_[PT_RAP_DATA] = "rap_data";
name_[PT_RAP_ACK] = "rap_ack";
name_[PT_TFRC]= "tcpFriend";
name_[PT_TFRC_ACK]= "tcpFriendCtl";
name_[PT_PING]="ping";
name_[PT_PBC] = "PBC";
/* For diffusion : Chalermek */
name_[PT_DIFF] = "diffusion";
// Link state routing updates
name_[PT_RTPROTO_LS] = "rtProtoLS";
// MPLS LDP packets
name_[PT_LDP] = "LDP";
// for GAF
name_[PT_GAF] = "gaf";
// RealAudio packets
name_[PT_REALAUDIO] = "ra";
//pushback
name_[PT_PUSHBACK] = "pushback";
#ifdef HAVE_STL
// for PGM
name_[PT_PGM] = "PGM";
#endif //STL
// LMS entries
name_[PT_LMS]="LMS";
name_[PT_LMS_SETUP]="LMS_SETUP";
name_[PT_SCTP]= "sctp";
name_[PT_SCTP_APP1] = "sctp_app1";
// smac
name_[PT_SMAC]="smac";
// HDLC
name_[PT_HDLC]="HDLC";
// XCP
name_[PT_XCP]="xcp";
// Bell Labs (PackMime OL)
name_[PT_BLTRACE]="BellLabsTrace";
name_[PT_NTYPE]= "undefined";
}
static void initName()
{
if(nPkt_ >= PT_NTYPE+1)
return;
char **nameNew = new char*[PT_NTYPE+1];
for(unsigned int i = (unsigned int)PT_SMAC+1; i < nPkt_; i++)
{
nameNew[i] = name_[i];
}
if(!nPkt_)
delete [] name_;
name_ = nameNew;
nPkt_ = PT_NTYPE+1;
name_[PT_TCP]= "tcp";
name_[PT_UDP]= "udp";
name_[PT_CBR]= "cbr";
name_[PT_AUDIO]= "audio";
name_[PT_VIDEO]= "video";
name_[PT_ACK]= "ack";
name_[PT_START]= "start";
name_[PT_STOP]= "stop";
name_[PT_PRUNE]= "prune";
name_[PT_GRAFT]= "graft";
name_[PT_GRAFTACK]= "graftAck";
name_[PT_JOIN]= "join";
name_[PT_ASSERT]= "assert";
name_[PT_MESSAGE]= "message";
name_[PT_RTCP]= "rtcp";
name_[PT_RTP]= "rtp";
name_[PT_RTPROTO_DV]= "rtProtoDV";
name_[PT_CtrMcast_Encap]= "CtrMcast_Encap";
name_[PT_CtrMcast_Decap]= "CtrMcast_Decap";
name_[PT_SRM]= "SRM";
name_[PT_REQUEST]= "sa_req";
name_[PT_ACCEPT]= "sa_accept";
name_[PT_CONFIRM]= "sa_conf";
name_[PT_TEARDOWN]= "sa_teardown";
name_[PT_LIVE]= "live";
name_[PT_REJECT]= "sa_reject";
name_[PT_TELNET]= "telnet";
name_[PT_FTP]= "ftp";
name_[PT_PARETO]= "pareto";
name_[PT_EXP]= "exp";
name_[PT_INVAL]= "httpInval";
name_[PT_HTTP]= "http";
name_[PT_ENCAPSULATED]= "encap";
name_[PT_MFTP]= "mftp";
name_[PT_ARP]= "ARP";
name_[PT_MAC]= "MAC";
name_[PT_TORA]= "TORA";
name_[PT_DSR]= "DSR";
name_[PT_AODV]= "AODV";
name_[PT_IMEP]= "IMEP";
name_[PT_RAP_DATA] = "rap_data";
name_[PT_RAP_ACK] = "rap_ack";
name_[PT_TFRC]= "tcpFriend";
name_[PT_TFRC_ACK]= "tcpFriendCtl";
name_[PT_PING]="ping";
name_[PT_PBC] = "PBC";
/* For diffusion : Chalermek */
name_[PT_DIFF] = "diffusion";
// Link state routing updates
name_[PT_RTPROTO_LS] = "rtProtoLS";
// MPLS LDP packets
name_[PT_LDP] = "LDP";
// for GAF
name_[PT_GAF] = "gaf";
// RealAudio packets
name_[PT_REALAUDIO] = "ra";
//pushback
name_[PT_PUSHBACK] = "pushback";
#ifdef HAVE_STL
// for PGM
name_[PT_PGM] = "PGM";
#endif //STL
// LMS entries
name_[PT_LMS]="LMS";
name_[PT_LMS_SETUP]="LMS_SETUP";
name_[PT_SCTP]= "sctp";
name_[PT_SCTP_APP1] = "sctp_app1";
// smac
name_[PT_SMAC]="smac";
// HDLC
name_[PT_HDLC]="HDLC";
// XCP
name_[PT_XCP]="xcp";
// Bell Labs (PackMime OL)
name_[PT_BLTRACE]="BellLabsTrace";
name_[PT_NTYPE]= "undefined";
}
Dans mon prog g écrit :
#include <string> // ou string.h
...
string Str = "Salut";
et je voudrais le caster en const char *
si tu ne comprend toujours pas donne moi ton e-mail et je t'envoie le source.
Merci ++
#include <string> // ou string.h
...
string Str = "Salut";
et je voudrais le caster en const char *
si tu ne comprend toujours pas donne moi ton e-mail et je t'envoie le source.
Merci ++
#include <iostream.h>
#include <stdlib.h>
#include <string>
#include <stdio.h> // fopen (), fflush ()
#include <conio.h> // getche ()
#include <ctype.h> // toupper ()
#include <time.h> // clock ()
namespace
{
int IndexCaracs;
char *TabCaracs;
string Nom;
bool Trouve = false;
const char *StrToChar (string Str)
{
char *Tab = (char *) malloc ((Str.length ()+1) * sizeof (char));
for (int i = 0; i < Str.length (); ++i) Tab [i] = Str [i];
Tab [Str.length ()] = '\0';
return Tab;
}
const char *Build (string Name, string Pass)
{
// unrar e -av- -c- -cu -p<mdp> -y <nom du rar>
string Command = "unrar e -av- -c- -cu -inul -p" + Pass + " -y " + Name;
return StrToChar (Command);
}
bool CheckYesNo (string Question)
{
char c;
for ( ; toupper (c) != 'O' && toupper (c) != 'N'; cout << '\n')
{
cout << Question << " [o/n] ? ";
c = getche ();
}
return (toupper (c) == 'O');
}
void SearchRec (string MdPInit, int NbRest)
{
if (Trouve) return;
if (!NbRest)
{
cout << "Test de : " << MdPInit << endl;
if (0 == system (Build (Nom, MdPInit)))
{
cout << " => OK ! " << endl;
Trouve = true;
}
return;
}
for (int i = 0; i < IndexCaracs; ++i)
SearchRec (MdPInit + TabCaracs [i], NbRest-1);
}
void Chrono (clock_t d, clock_t f)
{
int NbH, NbM, NbS;
NbS = (int) ((double) (f-d)/CLOCKS_PER_SEC);
NbH = (NbS - (NbS % 3600)) / 3600;
NbS -= NbH * 3600;
NbM = (NbS - (NbS % 60)) / 60;
NbS -= NbM * 60;
cout << "Crackage effectue en "
<< NbH << 'h' << NbM << 'm' << NbS << 's' << endl;
}
}
int main()
{
int NbCar, Temp;
char c;
string MotDePasse;
bool Majus, Minus, Number, Other;
FILE *fd;
clock_t Debut, Fin;
Majus = Minus = Number = Other = false;
NbCar = 0;
cout << " Extraction d'un fichier Winrar " << endl
<< " ------------------------------ " << endl << endl;
if (! (fd = fopen ("UnRAR.exe", "r")))
{
cerr << "/!\\ Pour que le programme fonctionne vous devez placer cet executable dans le" << endl
<< " repertoire de WinRAR" << endl << endl;
system ("PAUSE");
return -1;
}
fclose (fd);
cout << "Entrez le nom de votre archive (sans le \".rar\") : "; getline (cin, Nom); Nom += ".rar";
if (!(fd = fopen (StrToChar (Nom), "r")))
{
cerr << "/!\\ Pour que le programme fonctionne vous devez placer votre archive dans le" << endl
<< " repertoire de WinRAR" << endl << endl;
system ("PAUSE");
return -1;
}
fclose (fd);
while ((!Majus) && (!Minus) && (!Number) && (!Other))
{
Majus = CheckYesNo ("Tester les majuscules");
Minus = CheckYesNo ("Tester les minuscules");
Number = CheckYesNo ("Tester les chiffres");
Other = CheckYesNo ("Tester les autres caracteres imprimables");
if ((!Majus) && (!Minus) && (!Number) && (!Other))
cerr << "/!\\ Vous devez au moins examiner 1 type de caracteres" << endl;
}
while ((NbCar < 1) || (NbCar > 8))
{
cout << "Nombre max. de caracteres du mot de passe [<= 8] ? ";
NbCar = getche () - 48;
if ((NbCar < 1) || (NbCar > 8)) cerr << " /!\\ Nombre incorrect !" << endl;
}
cout << endl << endl;
fflush (stdin);
cout << "Archive : " << '"' << Nom << '"' << endl;
if (Majus) cout << "Tester les majuscules" << endl;
if (Minus) cout << "Tester les minuscules" << endl;
if (Number) cout << "Tester les chiffres" << endl;
if (Other) cout << "Tester les autres caracteres" << endl;
cout << "Mot de passe de " << NbCar << " caracteres maximum" << endl << endl;
system ("PAUSE");
// Début de la recherche du mot de passe...
IndexCaracs = 0; Temp = 0;
if (Majus) IndexCaracs += 26;
if (Minus) IndexCaracs += 26;
if (Number) IndexCaracs += 10;
if (Other) IndexCaracs += 27;
TabCaracs = (char *) malloc (IndexCaracs * sizeof (char));
if (Minus) for (int i = 0; i < 26; ++i) TabCaracs [Temp++] = char (97+i);
if (Majus) for (int i = 0; i < 26; ++i) TabCaracs [Temp++] = char (65+i);
if (Number) for (int i = 0; i < 10; ++i) TabCaracs [Temp++] = char (48+i);
if (Other)
for (int i = 33; i < 127; ++i)
{
if (i == 48) i = 58;
else if (i == 65) i = 91;
else if (i == 97) i = 123;
else if ((i == 34) || (i == 38) || (i == 45) || (i == 62) || (i == 94))
continue;
TabCaracs [Temp++] = char (i);
}
Debut = clock ();
for (int i = 1; i <= NbCar; ++i) SearchRec ("", i);
Fin = clock ();
cout << endl;
Chrono (Debut, Fin);
system ("PAUSE");
return 0;
}
#include <stdlib.h>
#include <string>
#include <stdio.h> // fopen (), fflush ()
#include <conio.h> // getche ()
#include <ctype.h> // toupper ()
#include <time.h> // clock ()
namespace
{
int IndexCaracs;
char *TabCaracs;
string Nom;
bool Trouve = false;
const char *StrToChar (string Str)
{
char *Tab = (char *) malloc ((Str.length ()+1) * sizeof (char));
for (int i = 0; i < Str.length (); ++i) Tab [i] = Str [i];
Tab [Str.length ()] = '\0';
return Tab;
}
const char *Build (string Name, string Pass)
{
// unrar e -av- -c- -cu -p<mdp> -y <nom du rar>
string Command = "unrar e -av- -c- -cu -inul -p" + Pass + " -y " + Name;
return StrToChar (Command);
}
bool CheckYesNo (string Question)
{
char c;
for ( ; toupper (c) != 'O' && toupper (c) != 'N'; cout << '\n')
{
cout << Question << " [o/n] ? ";
c = getche ();
}
return (toupper (c) == 'O');
}
void SearchRec (string MdPInit, int NbRest)
{
if (Trouve) return;
if (!NbRest)
{
cout << "Test de : " << MdPInit << endl;
if (0 == system (Build (Nom, MdPInit)))
{
cout << " => OK ! " << endl;
Trouve = true;
}
return;
}
for (int i = 0; i < IndexCaracs; ++i)
SearchRec (MdPInit + TabCaracs [i], NbRest-1);
}
void Chrono (clock_t d, clock_t f)
{
int NbH, NbM, NbS;
NbS = (int) ((double) (f-d)/CLOCKS_PER_SEC);
NbH = (NbS - (NbS % 3600)) / 3600;
NbS -= NbH * 3600;
NbM = (NbS - (NbS % 60)) / 60;
NbS -= NbM * 60;
cout << "Crackage effectue en "
<< NbH << 'h' << NbM << 'm' << NbS << 's' << endl;
}
}
int main()
{
int NbCar, Temp;
char c;
string MotDePasse;
bool Majus, Minus, Number, Other;
FILE *fd;
clock_t Debut, Fin;
Majus = Minus = Number = Other = false;
NbCar = 0;
cout << " Extraction d'un fichier Winrar " << endl
<< " ------------------------------ " << endl << endl;
if (! (fd = fopen ("UnRAR.exe", "r")))
{
cerr << "/!\\ Pour que le programme fonctionne vous devez placer cet executable dans le" << endl
<< " repertoire de WinRAR" << endl << endl;
system ("PAUSE");
return -1;
}
fclose (fd);
cout << "Entrez le nom de votre archive (sans le \".rar\") : "; getline (cin, Nom); Nom += ".rar";
if (!(fd = fopen (StrToChar (Nom), "r")))
{
cerr << "/!\\ Pour que le programme fonctionne vous devez placer votre archive dans le" << endl
<< " repertoire de WinRAR" << endl << endl;
system ("PAUSE");
return -1;
}
fclose (fd);
while ((!Majus) && (!Minus) && (!Number) && (!Other))
{
Majus = CheckYesNo ("Tester les majuscules");
Minus = CheckYesNo ("Tester les minuscules");
Number = CheckYesNo ("Tester les chiffres");
Other = CheckYesNo ("Tester les autres caracteres imprimables");
if ((!Majus) && (!Minus) && (!Number) && (!Other))
cerr << "/!\\ Vous devez au moins examiner 1 type de caracteres" << endl;
}
while ((NbCar < 1) || (NbCar > 8))
{
cout << "Nombre max. de caracteres du mot de passe [<= 8] ? ";
NbCar = getche () - 48;
if ((NbCar < 1) || (NbCar > 8)) cerr << " /!\\ Nombre incorrect !" << endl;
}
cout << endl << endl;
fflush (stdin);
cout << "Archive : " << '"' << Nom << '"' << endl;
if (Majus) cout << "Tester les majuscules" << endl;
if (Minus) cout << "Tester les minuscules" << endl;
if (Number) cout << "Tester les chiffres" << endl;
if (Other) cout << "Tester les autres caracteres" << endl;
cout << "Mot de passe de " << NbCar << " caracteres maximum" << endl << endl;
system ("PAUSE");
// Début de la recherche du mot de passe...
IndexCaracs = 0; Temp = 0;
if (Majus) IndexCaracs += 26;
if (Minus) IndexCaracs += 26;
if (Number) IndexCaracs += 10;
if (Other) IndexCaracs += 27;
TabCaracs = (char *) malloc (IndexCaracs * sizeof (char));
if (Minus) for (int i = 0; i < 26; ++i) TabCaracs [Temp++] = char (97+i);
if (Majus) for (int i = 0; i < 26; ++i) TabCaracs [Temp++] = char (65+i);
if (Number) for (int i = 0; i < 10; ++i) TabCaracs [Temp++] = char (48+i);
if (Other)
for (int i = 33; i < 127; ++i)
{
if (i == 48) i = 58;
else if (i == 65) i = 91;
else if (i == 97) i = 123;
else if ((i == 34) || (i == 38) || (i == 45) || (i == 62) || (i == 94))
continue;
TabCaracs [Temp++] = char (i);
}
Debut = clock ();
for (int i = 1; i <= NbCar; ++i) SearchRec ("", i);
Fin = clock ();
cout << endl;
Chrono (Debut, Fin);
system ("PAUSE");
return 0;
}
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
Disons que la classe string.h n'a pas été développée par mes soins, c'est celle de base du langage C++
Comment fais-tu toi pour déclarer une chaîne de caractères ?
Comment fais-tu toi pour déclarer une chaîne de caractères ?
perso je conné un peut la classe string ( l 'ami du programeur ) mais reussi a faire ce que tu veut !!!
sorry mais tu peut faire const string ( g pas trouver dans ton code ou tu veut faire sa si tu peut mettre que se que tu a besion de modifier ce serra plus facile pour les autre ;-)
sorry mais tu peut faire const string ( g pas trouver dans ton code ou tu veut faire sa si tu peut mettre que se que tu a besion de modifier ce serra plus facile pour les autre ;-)
Désolé d'avoir mis tout le code, je vais essayer de résumer :
En fait je suis en train de faire un prog censé casser les codes de WinRAR, en essayant tout simplement toutes les possibilités.
Donc je stocke la commande à exécuter dans une string de la classe standard string du langage C++ (à ne pas confondre avec le C...), puis je l'exécute avec un appel à la fonction system ().
Seulement celle-ci demande un const char * et non un char * tout simple (donc pas de string non plus).
J'aurais aimé savoir si c'était possible de faire un const_ cast sur ma chaine, et si oui de quelle manière l'écrire.
Le transtypage se fait au niveau de la fonction StrToChar, pour ceux qui veulent aller y jeter un coup d'oeil... (attention : ce que j'ai fait marche mais c'est 'artisanal', il me faudrait un moyen moins coûteux en temps CPU).
Voilà, j'espère avoir été clair.
Merci à tous de votre aide.
En fait je suis en train de faire un prog censé casser les codes de WinRAR, en essayant tout simplement toutes les possibilités.
Donc je stocke la commande à exécuter dans une string de la classe standard string du langage C++ (à ne pas confondre avec le C...), puis je l'exécute avec un appel à la fonction system ().
Seulement celle-ci demande un const char * et non un char * tout simple (donc pas de string non plus).
J'aurais aimé savoir si c'était possible de faire un const_ cast sur ma chaine, et si oui de quelle manière l'écrire.
Le transtypage se fait au niveau de la fonction StrToChar, pour ceux qui veulent aller y jeter un coup d'oeil... (attention : ce que j'ai fait marche mais c'est 'artisanal', il me faudrait un moyen moins coûteux en temps CPU).
Voilà, j'espère avoir été clair.
Merci à tous de votre aide.