首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >boost::asio::编写UNICODE

boost::asio::编写UNICODE
EN

Stack Overflow用户
提问于 2011-11-29 06:40:15
回答 2查看 1.7K关注 0票数 3

我在ANSII中有以下代码:

代码语言:javascript
复制
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。我尝试了以下几种方法:

代码语言:javascript
复制
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()?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-29 12:06:08

你需要知道你的数据是什么编码的。

例如,在我的应用程序中,我知道unicode数据是以UTF-8格式传入的,因此我使用函数的普通char版本。然后,我需要将缓冲区视为unicode utf-8数据-但一切都可以正常接收/发送。

如果您使用不同的字符编码,那么您可能(也可能不会)像您尝试过的那样,使用宽字符版本获得更好的里程数。

票数 3
EN

Stack Overflow用户

发布于 2011-11-29 10:58:04

我并不完全理解您在这里进行的所有调用(我自己最近才深入了解asio ),但我知道您可以非常简单地使用向量来处理数据。

举个例子,这就是我读取unicode文件并通过posix套接字传输的方法:

代码语言:javascript
复制
// 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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8303425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档