我在ANSII中有以下代码:
boost::asio::streambuf buffer;
std::ostream oss(&buffer);
boost::asio::async_write(socket_, buffer,
strand_.wrap(
boost::bind(&Connection::handleWrite, shared_from_this(),
boost::asio::placeholders::error)));我需要把它转换成UNICODE。我尝试了以下几种方法:
boost::asio::basic_streambuf<std::allocator<wchar_t>> buffer;
std::wostream oss(&buffer);
boost::asio::async_write(socket_, buffer,
strand_.wrap(
boost::bind(&Connection::handleWrite, shared_from_this(),
boost::asio::placeholders::error)));有没有办法在UNICODE中使用async_write()?
发布于 2011-11-29 12:06:08
你需要知道你的数据是什么编码的。
例如,在我的应用程序中,我知道unicode数据是以UTF-8格式传入的,因此我使用函数的普通char版本。然后,我需要将缓冲区视为unicode utf-8数据-但一切都可以正常接收/发送。
如果您使用不同的字符编码,那么您可能(也可能不会)像您尝试过的那样,使用宽字符版本获得更好的里程数。
发布于 2011-11-29 10:58:04
我并不完全理解您在这里进行的所有调用(我自己最近才深入了解asio ),但我知道您可以非常简单地使用向量来处理数据。
举个例子,这就是我读取unicode文件并通过posix套接字传输的方法:
// Open the file
std::ifstream is(filename, std::ios::binary);
std::vector<wchar_t> buffer;
// Get the file byte length
long start = is.tellg();
is.seekg(0, std::ios::end);
long end = is.tellg();
is.seekg(0, std::ios::beg);
// Resize the vector to the file length
buffer.resize((end-start)/sizeof(wchar_t));
is.read((char*)&buffer[0], end-start);
// Write the vector to the pipe
boost::asio::async_write(output, boost::asio::buffer(buffer),
boost::bind(&FileToPipe::handleWrite, this));对boost::asio::buffer(向量)的调用记录在这里:http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/reference/buffer/overload17.html
https://stackoverflow.com/questions/8303425
复制相似问题