我使用我們撰寫的霍夫曼編碼來壓縮檔案。該函式采用String,其輸出為String。
問題是我想將它保存為二進制檔案以獲得小于原始大小的大小,但是當我將它(0 和 1 )作為字串取回時,它的大小大于主檔案。如何將該字串(0 和 1)轉換為二進制,以便每個字符都保存為 1 位?我正在使用 Qt 來實作這一點:
string Huffman_encoding(string text)
{
buildHuffmanTree(text);
string encoded = "";
unordered_map<char, string> StringEncoded;
encoding(main_root, "", StringEncoded);
for (char ch : text) {
encoded = StringEncoded[ch];
}
return encoded;
}
uj5u.com熱心網友回復:
規范解決方案使用接受位串并發出打包位元組的“位打包器”。首先,替換encoded為以下實體:
class BitPacker {
QByteArray res;
quint8 bitsLeft = 8;
quint8 buf = 0;
public:
void operator =(const std::string& s) {
for (auto c : s) {
buf = buf << 1 | c - '0';
if (--bitsLeft == 0) {
res.append(buf);
buf = 0;
bitsLeft = 8;
}
}
}
QByteArray finish() {
if (bitsLeft < 8) {
res.append(buf << bitsLeft);
buf = 0;
bitsLeft = 8;
}
return res;
}
}
operator =將添加額外的位buf并將完整位元組重繪 到res. 在程序結束時,您可能會剩下 3 位。finish使用一個簡單的演算法:它用零填充緩沖區以生成最終位元組并將完全編碼的緩沖區回傳給您。
更復雜的解決方案可能是引入源字符集中不存在的顯式“流結束”標記。
uj5u.com熱心網友回復:
似乎您要搜索的是一種將包含 0 和 1 序列(如“0000010010000000”)的字串轉換為實際二進制表示(在本例中為數字 4 和 128)的方法。
這可以通過這樣的函式來實作:
#include <iostream>
#include <string>
#include <cstdint>
#include <vector>
std::vector<uint8_t> toBinary(std::string const& binStr)
{
std::vector<uint8_t> result;
result.reserve(binStr.size() / 8);
size_t pos = 0;
size_t len = binStr.length();
while (pos < len)
{
size_t curLen = std::min(static_cast<size_t>(8), len-pos);
auto curStr = binStr.substr(pos, curLen) std::string(8-curLen, '0');
std::cout << "curLen: " << curLen << ", curStr: " << curStr << "\n";
result.push_back(std::stoi(curStr, 0, 2));
pos = 8;
}
return result;
}
// test:
int main()
{
std::string binStr("000001001000000001");
auto bin = toBinary(binStr);
for (auto i: bin)
{
std::cout << static_cast<int>(i) << " ";
}
return 0;
}
輸出:
4 128 64
然后您可以對這些數字做任何您想做的事情,例如將它們寫入二進制檔案。
請注意,toBinary如上所示,如果不完整,則用零填充最后一個位元組。
uj5u.com熱心網友回復:
您可以使用這樣的按位邏輯創建位元流:
#include <cassert>
#include <string>
#include <stdexcept>
#include <vector>
auto to_bit_stream(const std::string& bytes)
{
std::vector<std::uint8_t> stream;
std::uint8_t shift{ 0 };
std::uint8_t out{ 0 };
// allocate enough bytes to hold the bits
// speeds up the code a bit
stream.reserve((bytes.size() 7) / 8);
// loop over all bytes
for (const auto c : bytes)
{
// check input
if (!((c == '0') || (c == '1'))) throw std::invalid_argument("invalid character in input");
// shift output by one to accept next bit
out <<= 1;
// keep track of number of shifts
// after 8 shifts a byte has been filled
shift ;
// or the output with a 1 if needed
out |= (c == '1');
// complete an output byte
if (shift == 8)
{
stream.push_back(out);
out = 0;
shift = 0;
}
}
return stream;
}
int main()
{
// stream is 8 bits per value, values 0,1,2,3
auto stream = to_bit_stream("00000000000000010000001000000011");
assert(stream.size() == 4ul);
assert(stream[0] == 0);
assert(stream[1] == 1);
assert(stream[2] == 2);
assert(stream[3] == 3);
return 0;
}
uj5u.com熱心網友回復:
使用 std::stoi()
int n = std::stoi("01000100", nullptr, 2);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/385157.html
上一篇:Qt6多執行緒問題
下一篇:在Qt6中使用OpenDDS
