Sending Unicode (win32) text packets over the network using winsock?
I am trying to send text packets over the network using winsock. However the text is stored as wchar_t and I need to be able to convert the text into byte (char) format for sending over the network, which will then be converted back to wchar_t.
I've experimented with using ostringstream and have converted the wchar_t string of mine to what looks like byte format, however when I try to go the reverse I get a load of gibberish.
I can't seem to find any answers whilst looking online, so 开发者_StackOverflow社区any help would be much appreciated.
OK here is some code that I played with.
std::wstring text( ieS("Hello") );
std::ostringstream ostr;
ostr.imbue( std::locale( ) );
ostr << text.c_str();
std::string text2 = ostr.str();
Convert to std::string to get char format.
std::wostringstream wostr;
wostr.imbue( std::locale( ) );
wostr << text2.c_str();
text = oss.str(); // gibberish
Convert back to std::wstring to get wchar_t format...
Your friends here are:
WideCharToMultiByte, passing the code page for UTF-8 as the MB code page.
MultiByteToWideChar, ditto.
Example reposted from here:
#include <locale>
#include <iostream>
#include <string>
#include <sstream>
using namespace std ;
wstring widen( const string& str )
{
wostringstream wstm ;
const ctype<wchar_t>& ctfacet =
use_facet< ctype<wchar_t> >( wstm.getloc() ) ;
for( size_t i=0 ; i<str.size() ; ++i )
wstm << ctfacet.widen( str[i] ) ;
return wstm.str() ;
}
string narrow( const wstring& str )
{
ostringstream stm ;
const ctype<char>& ctfacet =
use_facet< ctype<char> >( stm.getloc() ) ;
for( size_t i=0 ; i<str.size() ; ++i )
stm << ctfacet.narrow( str[i], 0 ) ;
return stm.str() ;
}
int main()
{
{
const char* cstr = "abcdefghijkl" ;
const wchar_t* wcstr = widen(cstr).c_str() ;
wcout << wcstr << L'\n' ;
}
{
const wchar_t* wcstr = L"mnopqrstuvwx" ;
const char* cstr = narrow(wcstr).c_str() ;
cout << cstr << '\n' ;
}
}
精彩评论