python zlib 输出到 c++细绳

发布于 2024-10-22 11:20:52 字数 864 浏览 4 评论 0原文

我正在尝试将 python zlib 的输出写入 C++ 字符串。这个问题与Python zlib非常相似输出,如何恢复 mysql utf-8 表? 然而,这里的区别是我的 python 脚本生成一个 C++ 头文件,其中压缩数据应存储为字符串。但是,由于特殊字符和原始字节,我无法让 C++ 将其读取为字符串。我无法将其写入文件然后返回到 C++ 程序中的原因是因为这可能是驱动程序组件,因此不允许读取文件。 这是我正在尝试的一个小例子。


compressed_string = zlib.compress("This is a huge string. Around 263KB")
fptr = open('my_header.h', 'w')

content = "#ifndef __HEADER_DEFS__\n\
#define __HEADER_DEFS__\n\
\n\
#include  \n\
\n\
std::string binary_compressed = \"%s\" \n\
\n\
#endif" % compressed_string

fptr.write(content)
fptr.close()

然而,我正在压缩的字符串是一个巨大的数据,与我在这里给出的示例不同,因此我将屏幕截图添加到我在实际示例中获得的字符类型。

请查看 http://tinypic.com/r/1078lxw/7截屏。 谢谢

I am trying to write the output of python zlib's output to a c++ string. The question is very similar to Python zlib output, how to recover out of mysql utf-8 table? here however, the difference is that my python script generates a C++ header file where the compressed data should be stored as a string. However, because of special characters and raw bytes I am unable to get C++ to read this as a string. The reason I cannot write it to a file and then it back in a c++ program is because this might be a driver component and hence reading files is not allowed.
Here is a small example of what I am trying.


compressed_string = zlib.compress("This is a huge string. Around 263KB")
fptr = open('my_header.h', 'w')

content = "#ifndef __HEADER_DEFS__\n\
#define __HEADER_DEFS__\n\
\n\
#include  \n\
\n\
std::string binary_compressed = \"%s\" \n\
\n\
#endif" % compressed_string

fptr.write(content)
fptr.close()

However, the string I am compressing is a huge data, unlike the example I have given here and hence I am adding a screenshot to the kind of characters I am getting in the actual example.

Please take a look at http://tinypic.com/r/1078lxw/7 for the screenshot.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窗影残 2024-10-29 11:20:52

您需要转义不可打印字符 在字符串中使用八进制表示法。

例如,

import string
....
safe_string = ""
for ch in compressed_string:
   if ch in string.printable and ch != '\\':
      safe_string += ch
   else:
      safe_string += "\%o" % ord(ch)
content = ".....\"%s\" ...." % safe_string

(建议避免十六进制表示法,因为它可以是可变长度,并且转义字符后面的可打印字符可能是合法的十六进制数字,这会破坏事物)

You need to escape the non-printable characters in the string using octal notation.

For example,

import string
....
safe_string = ""
for ch in compressed_string:
   if ch in string.printable and ch != '\\':
      safe_string += ch
   else:
      safe_string += "\%o" % ord(ch)
content = ".....\"%s\" ...." % safe_string

(It is adviced to avoid hex notation, as that can be variable length and printable characters that follow the escaped character may be legal hexadecimal digits, which will break things)

左岸枫 2024-10-29 11:20:52

如果您尝试将二进制数据嵌入到 C++ 程序中,Unix xxd -i 命令帮助?

这将生成一个 C 头文件,其中包含输入二进制文件的 char 数组表示形式。

If you're trying to embed binary data into a C++ program, would the Unix xxd -i command help?

That will generate a C header file containing a char array representation of a input binary file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文