boost::asio::写入 UNICODE

发布于 2024-12-18 06:28:59 字数 656 浏览 1 评论 0原文

我有以下 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 技术交流群。

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

发布评论

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

评论(2

暖树树初阳… 2024-12-25 06:28:59

您需要知道数据的编码形式。

例如,在我的应用程序中,我知道 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.

因为看清所以看轻 2024-12-25 06:28:59

我不太了解您在这里所做的所有调用(我自己最近才深入了解 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(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:

// 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));

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

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