ии 1
unknown
plain_text
2 years ago
1.1 kB
4
Indexable
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
setlocale(0, "Rus");
ofstream file("data.bin", ios::out | ios::binary);
if (!file.is_open()) {
cerr << "Ошибка при открытии файла" << endl;
exit(1);
}
string bytes="";
string lines[512];
cout << "Введите биты" << endl;
for (int i = 0; i<512; i++) {
cin >> lines[i];
for (int j = 0; j < lines[i].size(); j++) {
if ((lines[i][j] == '0' || lines[i][j] == '1') && j<32) bytes += lines[i][j];
else goto end;
}
}
end:
for (int i = 0; i < bytes.size() % 8; i++) {
bytes += '0';
}
cout << bytes << endl;
// Преобразуем последовательность нулей и единиц в байты
for (int i = 0; i < bytes.size(); i += 8) {
string byte_str = bytes.substr(i, 8);
char byte = stoi(byte_str, 0, 2);
file.write(&byte, 1);
}
// Закрываем файл
file.close();
cout << "Байты записаны" << endl;
system("pause");
return 0;
}Editor is loading...