关于在boost.python中将Python二进制转换为C++二进制的错误

0 投票
2 回答
735 浏览
提问于 2025-04-17 15:30

我需要把一些二进制数据从Python转换到C++,是通过boost::python这个工具来实现的。这些二进制数据可能来自图像文件或者文本文件。不过在把图像文件的二进制数据转换成C++的时候出现了一些错误。下面是一个例子。

C++代码:

#include <boost/python.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <fstream>
#include <iostream>

using namespace boost::python;
void greet(char *name,char *ss)
{
    std::ofstream fout(name,std::ios::binary);
    std::cout<< "This length is:" << strlen(ss) <<std::endl;
    fout.write(ss.strlen);
    fout.close();
    return;
}

BOOST_PYTHON_MODULE(ctopy)
{
    def("greet",greet);
}

Python代码:

import ctopy
#It is right.
f=open("t1.txt","rb")
ctopy.greet("t2.txt",f.read())
f.close()

#Do a error.There isn't data in the file "p2.jpg".
f2=open("p1.jpg","rb")
ctopy.greet("p2.jpg",f2.read()) #error.the file "p2.jpg" will be a empty file.
f2.close()

怎么把图像的二进制数据转换成C++呢?

2 个回答

1

一个二进制文件的编码通常受一些其他因素的影响,而不仅仅是编程语言,比如文件类型、操作系统等等。举个例子,在POSIX系统中,文本文件包含的字符是按行组织的,这在C++和Python中都是一样的。两个语言只需要使用适合该格式的编码就可以了。在这种情况下,把Python的二进制流转换成C++的二进制流并没有特别的过程,因为在这两种语言中,它们都是原始的字节流。

原始代码中有两个问题:

  • strlen()应该用来确定以空字符结束的字符串的长度。如果二进制文件中包含值为\0的字节,那么strlen()就无法返回数据的完整大小。
  • 因为使用了char*而不是std::string,所以数据的大小会丢失。建议使用std::string,因为它可以通过size()提供大小,并且允许字符串中包含空字符。另一种解决方案是明确提供数据的大小和数据一起。

下面是一个完整的Boost.Python示例:

#include <boost/python.hpp>
#include <fstream>
#include <iostream>

void write_file(const std::string& name, const std::string& data)
{
  std::cout << "This length is: " << data.size() << std::endl;
  std::ofstream fout(name.c_str(), std::ios::binary);
  fout.write(data.data(), data.size());
  fout.close();
}

BOOST_PYTHON_MODULE(example)
{
  using namespace boost::python;
  def("write", &write_file);
}

还有示例Python代码(test.py):

import example

with open("t1.txt", "rb") as f:
    example.write("t2.txt", f.read())

with open("p1.png", "rb") as f:
    example.write("p2.png", f.read())

使用方法是,我下载了这个图片并创建了一个简单的文本文件,然后用上面的代码创建它们的副本:

[twsansbury]$ wget http://www.boost.org/style-v2/css_0/get-boost.png -O p1.png >> /dev/null 2>&1
[twsansbury]$ echo "this is a test" > t1.txt
[twsansbury]$ ./test.py 
This length is: 15
This length is: 27054
[twsansbury]$ md5sum t1.txt t2.txt
e19c1283c925b3206685ff522acfe3e6  t1.txt
e19c1283c925b3206685ff522acfe3e6  t2.txt
[twsansbury]$ md5sum p1.png p2.png
fe6240bff9b29a90308ef0e2ba959173  p1.png
fe6240bff9b29a90308ef0e2ba959173  p2.png

md5校验和匹配,说明文件内容是一样的。

0

请提供真实的代码,最好是从你的代码中创建一个简单的示例。另外,你使用的是哪个版本的Python呢?无论如何,下面是你提供的代码中几个错误的地方:

  • 你应该使用 const,这在任何C++常见问题解答中都会提到。
  • 你在一个可能不是以零结尾的东西上使用了strlen(),而且这个东西中间可能还会有零。
  • 你应该使用std::string,这样即使里面有空字符也不会出问题。
  • 关闭文件是没必要的,这个在对象销毁时会自动完成。刷新和检查流状态才更重要。如果失败了,就抛出一个异常。
  • 去掉多余的返回语句,虽然不影响,但显得多余。
  • 阅读一下PEP 8。
  • 在读取文件时使用with语句。

撰写回答