boost::asio::写入 UNICODE
我有以下 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()?
I have the following code in 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)));
I need to convert it to UNICODE. I tried the following:
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)));
Is there a way to use async_write() in UNICODE?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要知道数据的编码形式。
例如,在我的应用程序中,我知道 unicode 数据以 UTF-8 形式传入,因此我使用函数的正常
char
版本。然后我需要将缓冲区视为 unicode utf-8 数据 - 但一切都可以正常接收/发送。如果您使用不同的字符编码,那么您可能(或可能不会)使用您尝试过的宽字符版本获得更好的里程。
You need to know what encoding your data is coming in as.
For example in my applications I know the unicode data is coming in as UTF-8, and so I use the normal
char
versions of the functions. I then need to treat the buffers as unicode utf-8 data - but everything is received / sent OK.If you're using a different character encoding then you may (or may not) get better milage using a wide character version like you have tried.
我不太了解您在这里所做的所有调用(我自己最近才深入了解 asio),但我知道您可以非常简单地用向量处理数据。
例如,这就是我为读取 unicode 文件并通过 posix 套接字传输所做的工作:
对 boost::asio::buffer(vector) 的调用记录在此处: http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/reference/buffer/overload17.html
I am not quite across all the calls you are making here (having only gotten deeply into asio recently myself) but I know you can simply handle the data with a vector very simply.
So for example this is what i have done for reading a unicode file and transmitting over a posix socket:
The call to boost::asio::buffer(vector) is documented here: http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/reference/buffer/overload17.html