在 C++ 中从 8 位 ASCII 字符创建 7 位 ASCII 文本字符串

Create string of 7-bit ASCII text from 8-bit ASCII chars in C++

本文关键字:ASCII 文本 字符串 创建 字符 C++ 中从      更新时间:2023-10-16

我希望创建一个最多包含 46 个八位字节的字符串,用 7 位 ASCII 字符填充。例如,对于字符串"Hello":

  1. 我取"H"的最后 7 位(0x48 - 100 1000(,并将其放在第一个八位组的前 7 位中。
  2. 我取下一个字符'e'(0x65 - 110 0101(,第一个位将转到第一个八位组的最后一位,然后它将填充八位组 2 的接下来 6 位。
  3. 重复 1-2 直到字符串结束,然后其余的八位字节将用 1 填充。

这是我的尝试,我已经做了很多工作,我已经尝试使用bitset,但似乎它不适合这项任务,因为我不必一直有46个八位字节。如果字符串可以容纳 12(或 24、36(个八位字节(其余部分只用 1 填充(,那么我不必使用 46。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
std::string a = "Hello";
int N = 0;
if (a.size() <= 11) {
// I'm supposed to implement some logic here to check if it
// will fit 12, 24, 36 or 46 octets but I will do it later.
N = 80;
}
std::vector<bool> temp(N);
int j = 0;
for (int i = 0; i < a.size(); i++) {
std::vector<bool> chartemp(a[i]);
cout << a[i] << "n";
cout << chartemp[0] << "n";
cout << chartemp[1] << "n";
cout << chartemp[2] << "n";
cout << chartemp[3] << "n";
cout << chartemp[4] << "n";
temp[j++] = chartemp[0];
temp[j++] = chartemp[1];
temp[j++] = chartemp[2];
temp[j++] = chartemp[3];
temp[j++] = chartemp[4];
temp[j++] = chartemp[5];
temp[j++] = chartemp[6];
}
for (int k = j; k < N; k++) {
temp[j++] = 1;
}
std::string s = "";
for (int l = 0; l <= temp.size(); l++)
{
if (temp[l]) {
s += '1';
}
else {
s += '0';
}
}    
cout << s << "n";
}

结果是

000000000000000000000000000000000001111111111111111111111111111111111111111111110

似乎您希望语句std::vector<bool> chartemp(a[i])a的第i个字符作为一系列位复制到向量中。然而,向量的构造函数将该值解释为初始大小,a[i]是相应字符的 ASCII 值(a(例如72'H'(。因此,您很有可能创建比预期更大的向量,每个位置都用false初始化。

相反,我建议使用位掩码:

temp[j++] = a[i] & (1 << 6);
temp[j++] = a[i] & (1 << 5);
temp[j++] = a[i] & (1 << 4);
temp[j++] = a[i] & (1 << 3);
temp[j++] = a[i] & (1 << 2);
temp[j++] = a[i] & (1 << 1);
temp[j++] = a[i] & (1 << 0);

而不是使用temp[j++],你可以使用temp.push_back(a[i] & (1 << 0)),从而也克服了用正确的大小初始化向量的需要。

尝试这样的事情:

#include <string>
#include <vector>
std::string stuffIt(const std::string &str, const int maxOctets)
{
const int maxBits = maxOctets * 8;
const int maxChars = maxBits / 7;
if (str.size() > maxChars)
{
// t0o many chars to stuff into maxOctes!
return "";
}
std::vector<bool> temp(maxBits);
int idx = temp.size()-1;
for (int i = 0; i < str.size(); ++i)
{
char ch = str[i];
for(int j = 0; j < 7; ++j)
temp[idx--] = (ch >> (6-j)) & 1;
}
int numBits = (((7 * str.size()) + 7) & ~7);
for (int i = (temp.size()-numBits-1); i >= 0; --i) {
temp[i] = 1;
}
std::string s;
s.reserve(temp.size());
for(int j = temp.size()-1; j >= 0; --j)
s.push_back(temp[j] ? '1' : '0');
return s;
}

stuffIt("Hello", 12)回报:

100100011001011101100110110011011110000011111111111111111111111111111111111111111111111111111111

stuffIt("Hello", 24)回报:

100100011001011101100110110011011110000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

stuffIt("Hello", 36)回报:

100100011001011101100110110011011110000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

stuffIt("Hello", 46)回报:

10010001100101110110011011001101111000001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

如果你想知道给定字符串需要多少个八位字节(不添加充满1s 的八位字节(,您可以使用以下公式:

const int numChars = str.size();
const int numBits = (numChars * 7);
const int bitsNeeded = ((numBits + 7) & ~7);
const int octetsNeeded = (bitsNeeded / 8);

如果你想要额外的1s,只需四舍五入octetsNeeded到所需的值(例如,12的下一个偶数倍(。