-
December 17th, 2010, 03:11 PM
#1
Is it possible to use WideCharToMultiByte with Letterlike Symbols [like "™"]?
Help!
I need to send a string over internet from a Windows [client side] system to a UNIX [server side] ...
In the client side I have a std::wstring data and I "translate" this data to an UTF-8 std::string before send it to avoid problems in server side. To do that, I´m using WideCharToMultiByte func [http://msdn.microsoft.com/en-us/libr...v=vs.85).aspx].
However, I´m facing a problem to send "Skype ™ 4.2.2". The following error is launched in server side:
"... a wide character has been encountered that can not be represented as a multibyte sequence (according to the current locale) [Skypeâ
¢ 4.1] : [Invalid or incomplete multibyte or wide cha ..."
I wrote a sample code to illustrate what I´m trying to do:
void main()
{
std::wstring wide = L"Skype ™ 4.2.2";
std::string normal = "";
char codePgBuf[4096];
wchar_t wcodePgBuf[4096];
if (WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), -1, codePgBuf, sizeof(codePgBuf), NULL, NULL) == 0)
printf("WC2MB failed for %u : '%S", GetLastError(), wide.c_str());
normal = codePgBuf;
FILE *pFile;
pFile = fopen("test.txt", "w");
if (pFile!=NULL)
{
fputs(normal.c_str(), pFile);
fclose (pFile);
}
}
The result of this program is an file [test.txt] with this content:
Skype â„¢ 4.2.2
*** is going on? What am I doing wrong? Any guess?
Is it possible to use WideCharToMultiByte with Letterlike Symbols?
Many thanks,
-
December 17th, 2010, 03:56 PM
#2
Re: Is it possible to use WideCharToMultiByte with Letterlike Symbols [like "™"]?
How many other forums did you hit all at once? ...
http://cboard.cprogramming.com/cplus...e-%99-%5D.html
gg
-
December 18th, 2010, 06:45 AM
#3
Re: Is it possible to use WideCharToMultiByte with Letterlike Symbols [like "™"]?
There is nothing wrong with the conversion in your code. The problem is in reading the text file. I suspect that you are using a basic text editor that assumes ASCII characters and can't correctly interpret the three bytes (â„¢) required to encode the trademark symbol in UTF-8. Try opening the file with a program - Microsoft Word, for example - that can correctly handle UTF-8 text files.
-
December 18th, 2010, 03:16 PM
#4
Re: Is it possible to use WideCharToMultiByte with Letterlike Symbols [like "™"]?
No, you must specify the encoding when using fopen with UTF strings.
fopen("newfile.txt", "rw, ccs=<encoding>");
http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx
Like this:
fopen("newfile.txt", "w, ccs=UTF-8");
-
December 18th, 2010, 07:43 PM
#5
Re: Is it possible to use WideCharToMultiByte with Letterlike Symbols [like "™"]?
>> No, you must specify the encoding when using fopen with UTF strings.
Not if you already have a UTF8 string that you want to write. When you use "css=", you can only use wide stream functions: fwprintf, fputws, etc... Then the CRT takes care of the BOM and UTF16LE -> UTFx conversions for you.
It's a nice convenience that the MS-CRT provides - which the Op could have utilized.
Other issues with the code:
- void main()
- implementation defined behavior due to non-basic character literals
- sending a non-basic / non-LC_CTYPE encoded string through fputs()
gg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|