#include #include <fstream> bool fileToString(const std::string& filename, std::string& content) { std::ifstream file(filename.c_str(), std::ios::binary); if (!file) return false; file.seekg(0, std::ios::end); long size = file.tellg(); file.seekg(0, std::ios::beg); char* page = new char[size]; file.read(page, size); file.close(); content.assign(page, size); delete[] page; return true; } int main(int argc, char** argv) { if (argc < 2) { std::cerr << "Usage: " << argv[0] << " <files>" << std::endl; return 1; } for (int i = 1; i < argc; ++i) { std::string content; if (!fileToString(argv[i], content)) { std::cerr << "Unable to open the file: " << argv[i] << std::endl; return 2; } std::cout << "content = " << content << std::endl << "size = " << content.size() << std::endl; } return 0; }
void SauveMonInt(const std::string &flnm, int MonInt) { std::ifstream file(flnm.c_str(), std::ios::binary); file.write(reinterpret_cast<char*>(&MonInt), sizeof(int)); file.close(); }
#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(); } int main() { //test(); const std::string binFormat = binaryFormat(fileToString("testing.txt")); std::cout << binFormat << std::endl; std::cout << restoreFormat(binFormat) << std::endl; return 0; }
#include #include <fstream> #include <sstream> #include static const unsigned int SHIFT = 8; 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 = new char[nbOctet]; fichier.read(tableau, nbOctet); fichier.close(); text.assign(tableau, nbOctet); delete[] 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; } void applyXor(std::string& text) { const unsigned int size = text.size(); for (unsigned int i = 0; i < size; ++i) text[i] ^= 1; // autre exemple: text[i] -= 1 ou text[i] += 1, etc... } int main() { const std::string inputFile = "in"; const std::string outputFile2 = "out"; std::string text; if (!fileToString(inputFile, text)) { std::cerr << "Impossible d'ouvrir le fichier en lecture !" << std::endl; return 1; } applyXor(text); if (!stringToFile(outputFile2, text)) { std::cout << "Erreur sur fichier de sortie" << std::endl; return 3; } return 0; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questioncar traduire des caractères en octets selon la langue
char MyChar = 't'; int Myint = MyChar; // pas de conversion nécessaire ... std::cout<<"Tint:"<<Tint<<std::endl; // Sortie: Tint:116
char C = 310;//pas possible
#include #include <fstream> #include <cmath> #include <cstring> #include <sstream> using namespace std; int main() { ifstream fichier("testing.txt",ios::binary); if (!fichier.is_open()) //vérification cout << "Impossible d'ouvrir le fichier en lecture !"; int nbrOctet; fichier.seekg(0,ios_base::end); nbrOctet= fichier.tellg(); fichier.seekg(0,ios_base::beg); // construction du tableau de char, char* tableau = new char[nbrOctet]; fichier.read(tableau, nbrOctet); fichier.close(); int i=0, valeurChar=0,j, d; string a="0"; string b="1"; string s; int totalTest1=0; for(i=0; i<nbrOctet; i++) { valeurChar=tableau[i]; totalTest1=totalTest1+valeurChar; for(j=7;j>-1;j--) // convertion décimal vers binaire { d=valeurChar-pow(2,j); if(d>=0) {s=s+b; valeurChar=d;} else {s=s+a; } } } // s contient maintenant tout en binaire string aa, g; // string s est là où est stocké la chaine binaire, doit être un multiple de 8 si travail sur octet int valeurCharA=0, tailleStringS=0, bb=0,f=0, gchiffre=0,jj,ii,ff,hh; tailleStringS=s.size(); f=tailleStringS/8; ff=f + 1; //Il ne faut que des multiples de 8, + 1 car on démarre de 1 et non de 0! char* tableauSortie = new char[f]; char tableauAvent; for(jj=1;jj<f; jj++) //Il ne faut que des multiples de 8 { bb=8*jj -1; aa=s.substr(bb,8); valeurCharA=0; //initialisation pour éviter erreur de valeur for(ii=7;ii>-1;ii--) // on lit en binaire de droite à gauche!!! { g=aa.at(ii); { istringstream istr(g); istr >> gchiffre; } if(gchiffre==1) { hh=7-ii; // pr avoir le bon ii à la bonne position! valeurCharA= valeurCharA + pow(2,hh); } } tableauAvent= valeurCharA; //convertion du int en char tableauSortie[jj]=tableauAvent; // remplissage du tableau de sortie cout<<"\n int m= "<<m<; } //fin du remplisage du tableau de char //début de la copie du tableau ds le dossier ofstream fichierSortie ("testingSortie.txt", ios::out | ios::binary); //ouverture en ecriture !! fichierSortie.write(tableauSortie, f); fichierSortie.close(); cout<<"TERMINE !!!!!"; return 0; }
#include #include <fstream> int main() { std::ifstream fichier("testing.txt", std::ios::binary); if (!fichier.is_open()) { std::cerr << "Impossible d'ouvrir le fichier en lecture !" << std::endl; return 1; } 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();// Pas obligatoire, le fichie se ferme à la desctruction de l'objet fichier const std::string buff = tableau; // buff contient ton fichier en binaire. std::ofstream fichierSortie ("testingSortie.txt", std::ios::binary); fichierSortie.write(buff.c_str(), buff.size()); fichierSortie.close(); // Pas obligatoire, le fichie se ferme à la desctruction de l'objet fichierSortie std::cout << "TERMINE !!!!!" << std::endl; return 0; }