Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem when client send a chinese text #4

Open
shuidong opened this issue Dec 24, 2012 · 2 comments
Open

Problem when client send a chinese text #4

shuidong opened this issue Dec 24, 2012 · 2 comments

Comments

@shuidong
Copy link

simply I wrote some code to send message in chinese

std::string msg;
msg = "汉字";
handler->emit("test", msg);.

And here occur a exception like below:

boost::exception_detail::clone_implboost::exception_detail::error_info_injector<boost::system::system_error >。

@shuidong
Copy link
Author

i added method below, and solve that problem.

inline void WStrToUTF8(std::string& dest, const std::wstring& src){
dest.clear();
for (size_t i = 0; i < src.size(); i++){
wchar_t w = src[i];
if (w <= 0x7f)
dest.push_back((char)w);
else if (w <= 0x7ff){
dest.push_back(0xc0 | ((w >> 6)& 0x1f));
dest.push_back(0x80| (w & 0x3f));
}
else if (w <= 0xffff){
dest.push_back(0xe0 | ((w >> 12)& 0x0f));
dest.push_back(0x80| ((w >> 6) & 0x3f));
dest.push_back(0x80| (w & 0x3f));
}
else if (sizeof(wchar_t) > 2 && w <= 0x10ffff){
dest.push_back(0xf0 | ((w >> 18)& 0x07)); // wchar_t 4-bytes situation
dest.push_back(0x80| ((w >> 12) & 0x3f));
dest.push_back(0x80| ((w >> 6) & 0x3f));
dest.push_back(0x80| (w & 0x3f));
}
else
dest.push_back('?');
}
}

inline std::string WStrToUTF8(const std::wstring& str){
std::string result;
WStrToUTF8(result, str);
return result;
}

///
std::string msg;
msg = WStrToUTF8(L"汉字");
handler->emit("test", msg);.

@ebshimizu
Copy link
Owner

Looks like boost had a problem with those characters. I wonder if there's an easier way to do that, since the boost libraries probably have a way to handle different string encodings. I'll let you know if I find anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants