Logiquement mon code marche
g=aa.at(ii);
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionC'est faux a coup sur, std::string.at() renvoie un char... du coup g ne dépassera pas 1 caractère.
J'étais entrain de pondre les fonctions "charenoctet" "octetenchar" pour les poster quand j'ai vu que Pingu m'avait pris de vitesse.
très joli ton code Pingu c'est très pro
ça reste du charabia pour moi
un système de cryptage
qui contient les bits de ma chaine, comme pour prendre les bits d'un fichier et les mettre dans un string mais ici l'inverse)
// Met l'intégralité d'un texte dans un fichier bool stringToFile(const std::string& filename, const std::string& text) { // A toi de jouer. // Return true si l'écriture à réussi. }
// tout ce qui suit vient à la suite de ton code, dans le int main() sans les cout // je chiffre mon binFormat dans une autre variable "chiffrement" et m'assure que ça taille soit un multiple de 8 std::string chiffrement; chiffrement=binFormat; // Blabla, je commence à chiffrer avec la variable string chiffrement // les choses sérieuses commencent ! bool stringToFile(const std::string& filename, const std::string& text) // text a déjà été initialisé dans restoreFormat non ? { std::ofstream file(filename.c_str(), std::ios::binary); // je ne vois pas à quoi sert "filename.c_str()", un lien avec mon fichier de sortie mais .... si je veux que mon fichier de sortie soit différent de l'entrée ? du genre TestingEntrée.txt et testingSortie.txt ? if (!file) return false; // A toi de jouer. restoreFormat(chiffrement); // tous les caractères se retrouvent dans le buffer c'est ça? int t=chiffrement.size(); char tableauSortie[t]; fichier.write(tableauSortie, t); fichier.close(); // Return true si l'écriture à réussi. return true; }
bool stringToFile(const std::string& filename, const std::string& text) { std::ofstream file(filename.c_str(), std::ios::binary); if (!file) return false; fichier.write(text.c_str(), text.size()); fichier.close(); // facultatif, un fichier se ferme tout seul avec un std::ofstream. return true; } int main() { std::string chiffrement = binaryFormat("ton texte"); if (!stringToFile(chiffrement)) { // Erreur } // Ou pour la version "déchiffrée" if (!stringToFile(restoreFormat(chiffrement))) { // Erreur } return 0; }
J'ai vraiment l'impression que tu n'est pas encore à l'aise avec les fonctions. (...) Enfin, ne mélange pas les logiques dans les fonctions.
std::string chiffrement = binaryFormat("ton texte");
#include #include <fstream> #include <sstream> #include static const unsigned int SHIFT = 8; std::string formatCharToByte(unsigned char value) { const int shift = SHIFT - 1; const unsigned int mask = 1 << shift; std::ostringstream buff; for (int i = 1; i <= shift + 1; ++i) { buff << (value & mask ? '1' : '0'); value <<= 1; } return buff.str(); } unsigned char formatByteToChar(const std::string& binFormat) { unsigned char res = 0; const int size = binFormat.size() - 1; for (int i = 0; i <= size; i++) { int b = 1; int n = binFormat[i] - '0'; b <<= size - i; res += n * b; } return res; } void test() { for (char c = 'a'; c <= 'z'; ++c) std::cout << c << " -> " << formatCharToByte(c) << " -> " << formatByteToChar(formatCharToByte(c)) << std::endl; } std::string fileToString(const std::string& filename) { std::ifstream fichier(filename.c_str(), std::ios::binary); if (!fichier.is_open()) { std::cerr << "Impossible d'ouvrir le fichier en lecture !" << std::endl; return ""; } fichier.seekg(0, std::ios_base::end); const int nbOctet = fichier.tellg(); fichier.seekg(0, std::ios_base::beg); char tableau[nbOctet]; fichier.read(tableau, nbOctet); fichier.close(); return tableau; } std::string binaryFormat(const std::string& text) { std::ostringstream buff; for (size_t i = 0; i < text.size(); ++i) buff << formatCharToByte(text[i]); return buff.str(); } std::string restoreFormat(const std::string& text) { std::ostringstream buff; for (size_t i = 0; i < text.size(); i += SHIFT) buff << formatByteToChar(text.substr(i, SHIFT)); return buff.str(); } bool stringToFile(const std::string& filename, const std::string& text) { std::ofstream file(filename.c_str(), std::ios::binary); if (!file) return false; fichier.write(text.c_str(), text.size()); fichier.close(); // facultatif, un fichier se ferme tout seul avec un std::ofstream. return true; } int main() { //ouverture d'un fichier et obtention de ses 0 et 1 dans un string const std::string chiffrement = binaryFormat(fileToString("testingEntree.txt")); if (!fileToString("testingEntree.txt", chiffrement) ) { stt::cout<<"Erreur sur fichier d'entree"<< std::endl; } //je chiffre alors la variable string chiffrement. Pr préciser, je chiffre et créer un autre fichier qui contient le chiffrement. // je mets la variable string chiffrement qui vient d'être chiffrée stringToFile("testingSortie.txt", restoreFormat(chiffrement)); // transforme mon string en fichier de sortie. pas sur de moi là ... if (!stringToFile("testingSortie.txt", restoreFormat(chiffrement))) { stt::cout<<"Erreur sur fichier de sortie"<< std::endl; } return 0; }
Néanmoins ça reste juste
Je te conseille de travailler le découpage en fonctions, puis en class et enfin les types de données, les tableaux, et les pointeurs et références.
J'ai assez cherché ! Une réponse entière svp!
#include #include <fstream> #include <sstream> #include static const unsigned int SHIFT = 8; std::string formatCharToByte(unsigned char value) { const int shift = SHIFT - 1; const unsigned int mask = 1 << shift; std::ostringstream buff; for (int i = 1; i <= shift + 1; ++i) { buff << (value & mask ? '1' : '0'); value <<= 1; } return buff.str(); } unsigned char formatByteToChar(const std::string& binFormat) { unsigned char res = 0; const int size = binFormat.size() - 1; for (int i = 0; i <= size; i++) { int b = 1; int n = binFormat[i] - '0'; b <<= size - i; res += n * b; } return res; } bool fileToString(const std::string& filename, std::string& text) { std::ifstream fichier(filename.c_str(), std::ios::binary); if (!fichier) { return false; } fichier.seekg(0, std::ios_base::end); const int nbOctet = fichier.tellg(); fichier.seekg(0, std::ios_base::beg); char tableau[nbOctet]; fichier.read(tableau, nbOctet); fichier.close(); text.assign(tableau); return true; } bool stringToFile(const std::string& filename, const std::string& text) { std::ofstream file(filename.c_str(), std::ios::binary); if (!file) return false; file.write(text.c_str(), text.size()); file.close(); return true; } std::string binaryFormat(const std::string& text) { std::ostringstream buff; for (size_t i = 0; i < text.size(); ++i) buff << formatCharToByte(text[i]); return buff.str(); } std::string restoreFormat(const std::string& text) { std::ostringstream buff; for (size_t i = 0; i < text.size(); i += SHIFT) buff << formatByteToChar(text.substr(i, SHIFT)); return buff.str(); } int main() { const std::string inputFile = "testingEntree.txt"; const std::string outputFile1 = "testingSortie1.txt"; const std::string outputFile2 = "testingSortie2.txt"; std::string text; if (!fileToString(inputFile, text)) { std::cerr << "Impossible d'ouvrir le fichier en lecture !" << std::endl; return 1; } const std::string chiffrement = binaryFormat(text); if (!stringToFile(outputFile1, chiffrement)) { std::cout << "Erreur sur fichier de sortie" << std::endl; return 2; } if (!stringToFile(outputFile2, restoreFormat(chiffrement))) { std::cout << "Erreur sur fichier de sortie" << std::endl; return 3; } return 0; }
#include #include <fstream> #include <sstream> #include static const unsigned int SHIFT = 8; std::string formatCharToByte(unsigned char value) { const int shift = SHIFT - 1; const unsigned int mask = 1 << shift; std::ostringstream buff; for (int i = 1; i <= shift + 1; ++i) { buff << (value & mask ? '1' : '0'); value <<= 1; } return buff.str(); } unsigned char formatByteToChar(const std::string& binFormat) { unsigned char res = 0; const int size = binFormat.size() - 1; for (int i = 0; i <= size; i++) { int b = 1; int n = binFormat[i] - '0'; b <<= size - i; res += n * b; } return res; } bool fileToString(const std::string& filename, std::string& text) { std::ifstream fichier(filename.c_str(), std::ios::binary); if (!fichier) { return false; } fichier.seekg(0, std::ios_base::end); const int nbOctet = fichier.tellg(); fichier.seekg(0, std::ios_base::beg); char tableau[nbOctet]; fichier.read(tableau, nbOctet); fichier.close(); text.assign(tableau); return true; } bool stringToFile(const std::string& filename, const std::string& text) { std::ofstream file(filename.c_str(), std::ios::binary); if (!file) return false; file.write(text.c_str(), text.size()); file.close(); return true; } std::string binaryFormat(const std::string& text) { std::ostringstream buff; for (size_t i = 0; i < text.size(); ++i) buff << formatCharToByte(text[i]); return buff.str(); } std::string restoreFormat(const std::string& text) { std::ostringstream buff; for (size_t i = 0; i < text.size(); i += SHIFT) buff << formatByteToChar(text.substr(i, SHIFT)); return buff.str(); } int main() { const std::string inputFile = "testingEntree.txt"; const std::string outputFile1 = "testingSortie1.txt"; const std::string outputFile2 = "testingSortie2.txt"; std::string text; if (!fileToString(inputFile, text)) { std::cerr << "Impossible d'ouvrir le fichier en lecture !" << std::endl; return 1; } const std::string chiffrement = binaryFormat(text); if (!stringToFile(outputFile1, chiffrement)) { std::cout << "Erreur sur fichier de sortie" << std::endl; return 2; } // ici je peux mettre mon code de chiffrement sans problème hein ?! if (!stringToFile(outputFile2, restoreFormat(chiffrement))) { std::cout << "Erreur sur fichier de sortie" << std::endl; return 3; } return 0; }
En sommes, j'ai pas besoin du fichier de sortie avec les binaires en caractère.
// ici je peux mettre mon code de chiffrement sans problème hein ?!
La taille limite d'un fichier que je pourrais traiter selon la ram.