支持将正常的字符串转换为67语言和反操作。
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
int main() {
string input;
cout << "请输入字符串:";
getline(cin, input);
cout << "转换结果:" << endl;
for (unsigned char c : input) {
string binary = bitset<8>(c).to_string();
// 将 0 替换为 6,将 1 替换为 7
for (char &ch : binary) {
if (ch == '0') {
ch = '6';
} else if (ch == '1') {
ch = '7';
}
}
cout << binary << " ";
}
cout << endl;
return 0;
}
#include <iostream>
#include <string>
#include <bitset>
#include <stdexcept>
using namespace std;
int main() {
string input;
cout << "请输入编码后的字符串:";
getline(cin, input);
string binary;
string result;
// 去除空格,并将 6、7 还原为 0、1
for (char c : input) {
if (c == '6') {
binary += '0';
} else if (c == '7') {
binary += '1';
} else if (c != ' ') {
cout << "错误:输入只能包含 6、7 和空格!" << endl;
return 1;
}
}
// 每 8 位二进制转换为一个字节
if (binary.size() % 8 != 0) {
cout << "错误:二进制长度必须是 8 的倍数!" << endl;
return 1;
}
for (size_t i = 0; i < binary.size(); i += 8) {
string byteStr = binary.substr(i, 8);
unsigned long value = bitset<8>(byteStr).to_ulong();
result += static_cast<char>(value);
}
cout << "还原后的字符串:" << result << endl;
return 0;
}