在此示例中,
*只是将const char *值作为输入(即:: test one)
*将const char *转换为std :: string
*传递了std :: string的值来压缩字符串
*压缩字符串并且能够看到压缩字符串的值
* eg :: compressed String ::x + I-.Q Ko#
*现在将压缩数据的std :: string转换为const char * ...
*当我这样做时,只获得75%的数据。这些数据丢失了。
例如::x + I-.Q K
*我需要做什么才能获得整个数据?
*有人可以在这方面帮助我吗?
int main ( int argc,char *argv[] )
{
const char *str = "test one";
int length = 0;
std::string compressionString(str);//converting char * to std::string
cout << "compressionString::" << compressionString<< endl;
while(*(str+length))
length++; //length of char *
cout << "Length of entered string:: " <<length << endl;
std::string compStr = compress_string(compressionString);//Compress the string using compress_string()
cout << "compressed String::"<<compStr << endl;
const char* m_pcompCharptr = compStr.c_str();
cout << "Compressed string char* length::"<< sizeof(m_pcompCharptr) <<endl;
cout << "Compressed value which is present in char * is :: " <<m_pcompCharptr << endl;
cout << "Compressed String length:: " << compStr.length() << endl;
int iCompLen = compStr.length();
cout << "iCompLen::" << iCompLen << endl;
std::string decompressionString(m_pcompCharptr);//converting const char* to std::string
std::string decompStr = decompress_string(decompressionString);
cout << "decompStr::" << decompStr << endl;
return 0;
}
输出:
compressionString :: test one
输入字符串的长度:: 8
压缩字符串::x + I-.Q Ko#
压缩字符串char * levgth :: 8
char *中存在的压缩值是::x + I-.Q K
压缩字符串长度:: 16
iCompLen :: 16
在抛出'std :: runtime_error'的实例后终止调用 what():zlib解压缩期间的异常:( - 5) 中止(核心倾销)
答案 0 :(得分:0)
当你这样做时......
const char* m_pcompCharptr = compStr.c_str();
...你得const char*
compStr
中的第一个字符,它本身没有概念或长度/大小的记录。然后,如果您使用上面的const char*
构建一个字符串...
std::string decompressionString(m_pcompCharptr);
std::string
构造函数对const char*
的重载依赖于ASCIIZ编码来检测传入字符串的长度:这意味着它认为文本在第一个NUL字符处完成。
这不是错误......在处理二进制字符串时,您确实需要保留const char*
的{{1}}和size_t
值(您有效)在.size()
中捕获,以便像这样重建length
:
string