在 C++ 中从 8 位 ASCII 字符创建 7 位 ASCII 文本字符串
Create string of 7-bit ASCII text from 8-bit ASCII chars in C++
我希望创建一个最多包含 46 个八位字节的字符串,用 7 位 ASCII 字符填充。例如,对于字符串"Hello":
- 我取"H"的最后 7 位(0x48 - 100 1000(,并将其放在第一个八位组的前 7 位中。
- 我取下一个字符'e'(0x65 - 110 0101(,第一个位将转到第一个八位组的最后一位,然后它将填充八位组 2 的接下来 6 位。
- 重复 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
如果你想知道给定字符串需要多少个八位字节(不添加充满1
s 的八位字节(,您可以使用以下公式:
const int numChars = str.size();
const int numBits = (numChars * 7);
const int bitsNeeded = ((numBits + 7) & ~7);
const int octetsNeeded = (bitsNeeded / 8);
如果你想要额外的1
s,只需四舍五入octetsNeeded
到所需的值(例如,12的下一个偶数倍(。
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 输出使用 cout 转换为 ASCII 的文本时输出格式不正确,C++
- 在 C++ 中从 8 位 ASCII 字符创建 7 位 ASCII 文本字符串
- 使用 std::ifstream 读取 ASCII 文本文件C++
- 要求用户在C++中创建文本文件时输入文本文件(ASCII)名称
- 关于在C++中通过ASCII将文本转换为十六进制
- 字符类型并将 ASCII 文本重新编码为 UTF-16
- C++使用缓冲区写入文本时,在文件末尾返回非 ASCII 字符
- 从文本到ASCII的while循环错误
- 将 ASCII 值直接转换为文本整数值
- 写入文本文件,二进制与ascii
- 如何从迷宫ASCII文本文件中创建动态分配的2D数组?