如何保存(和检索)以归档位序列
How to save (and retrieve) to file a sequence of bits
我正在尝试在文件中存储一个位序列。
我尽量只描述最重要的部分:
- 我有一个向量(我知道,这不是个好主意,但我只是简单地使用它)
- 我想将它存储在一个文件中(我正在使用 Linux)
- 我想从所述文件中检索它并重新创建向量
由于 C++ 不允许存储单个位,我不得不将 char 中的所有位分组并将 char 保存为 "text"。
为此,我使用了 http://www.avrfreaks.net/forum/tut-c-bit-manipulation-aka-programming-101
如果位数是8的倍数,一切正常。
如果不是这样,我不知道如何处理这个问题。
我会更好地解释。我有:
010011000110111101110010011001010110110101
我将字符保存为:
01001100 -> L
01101111 -> o
01110010 -> r
01100101 -> e
01101101 -> m
01
最后一个“01”...我不知道如何存储它。
当然我可以创建一个带有 1 和一些 0 填充的字节......但是当我检索它们时我不知道 "extra bits" 的数量!
什么是填充,什么是信息?
我根本不知道该怎么做...有什么想法吗?
文件编写器的一些代码(不是我的实际代码...它太长...我只写了重要的部分...):
void Compressor::compress(std::istream &is, std::ostream &os) {
queue<bool> bit_buffer;
char c;
while (is.get(c)) {
new_letter = c;
const std::vector<bool> bit_c = char2bits(new_letter);
for(bool bit : bit_c)
bit_buffer.push(bit);
}
//Here my code adds a certain number of bits, I simulate this with:
bit_buffer.push(false);
bit_buffer.push(true);
// Write the bit buffer into a file
while (bit_buffer.size() >= 8) {
// Group vector<bool> in char
char output = 0;
for (int i=0; i<8; i++) {
int bit = bit_buffer.front();
bit_buffer.pop();
if (bit) bit_set(output, BIT(i));
else bit_clear(output, BIT(i));
}
// Individually write chars in file
os.write(&output,sizeof(char));
}
//????????
//Last bits???
//????????
}
vector<bool> char2bits (char c) {
bitset<8> bit_c (c);
vector<bool> bool_c;
for (int i=7; i>=0; i--) {
bool_c.push_back(bit_c[i]);
}
return bool_c;
}
进行位填充的一种方法是使用 10...0
.
进行填充
所以 01
被填充到 01100000
。
解码时忽略最后1
.
后面的一切
如果你最后有一个完整的字节,用10000000
填充。
我也会使用 header,但用于一组有效负载字节。
我的意思是:
HH PP PP PP PP PP PP ..
HH PP PP
- 如果HH == 256,好的,你有32位的Payload然后你会发现
另一个块
- 如果 HH < 256,您必须读取下一个 int(HH/8) 字节和最后一个字节一、HH %8位。另外,就是文件结束了。
对于较大的位字段,您可以将 header 增加到 16 或 32 位。
我正在尝试在文件中存储一个位序列。
我尽量只描述最重要的部分:
- 我有一个向量(我知道,这不是个好主意,但我只是简单地使用它)
- 我想将它存储在一个文件中(我正在使用 Linux)
- 我想从所述文件中检索它并重新创建向量
由于 C++ 不允许存储单个位,我不得不将 char 中的所有位分组并将 char 保存为 "text"。 为此,我使用了 http://www.avrfreaks.net/forum/tut-c-bit-manipulation-aka-programming-101
如果位数是8的倍数,一切正常。 如果不是这样,我不知道如何处理这个问题。
我会更好地解释。我有:
010011000110111101110010011001010110110101
我将字符保存为:
01001100 -> L
01101111 -> o
01110010 -> r
01100101 -> e
01101101 -> m
01
最后一个“01”...我不知道如何存储它。 当然我可以创建一个带有 1 和一些 0 填充的字节......但是当我检索它们时我不知道 "extra bits" 的数量! 什么是填充,什么是信息?
我根本不知道该怎么做...有什么想法吗?
文件编写器的一些代码(不是我的实际代码...它太长...我只写了重要的部分...):
void Compressor::compress(std::istream &is, std::ostream &os) {
queue<bool> bit_buffer;
char c;
while (is.get(c)) {
new_letter = c;
const std::vector<bool> bit_c = char2bits(new_letter);
for(bool bit : bit_c)
bit_buffer.push(bit);
}
//Here my code adds a certain number of bits, I simulate this with:
bit_buffer.push(false);
bit_buffer.push(true);
// Write the bit buffer into a file
while (bit_buffer.size() >= 8) {
// Group vector<bool> in char
char output = 0;
for (int i=0; i<8; i++) {
int bit = bit_buffer.front();
bit_buffer.pop();
if (bit) bit_set(output, BIT(i));
else bit_clear(output, BIT(i));
}
// Individually write chars in file
os.write(&output,sizeof(char));
}
//????????
//Last bits???
//????????
}
vector<bool> char2bits (char c) {
bitset<8> bit_c (c);
vector<bool> bool_c;
for (int i=7; i>=0; i--) {
bool_c.push_back(bit_c[i]);
}
return bool_c;
}
进行位填充的一种方法是使用 10...0
.
所以 01
被填充到 01100000
。
解码时忽略最后1
.
如果你最后有一个完整的字节,用10000000
填充。
我也会使用 header,但用于一组有效负载字节。 我的意思是:
HH PP PP PP PP PP PP ..
HH PP PP
- 如果HH == 256,好的,你有32位的Payload然后你会发现 另一个块
- 如果 HH < 256,您必须读取下一个 int(HH/8) 字节和最后一个字节一、HH %8位。另外,就是文件结束了。
对于较大的位字段,您可以将 header 增加到 16 或 32 位。