Is it possible to use WideCharToMultiByte with Letterlike Symbols [like ""]?

This is a discussion on Is it possible to use WideCharToMultiByte with Letterlike Symbols [like ""]? within the C++ Programming forums, part of the General Programming Boards category; Help! I need to send and string over internet from a Windows [client side] system to a UNIX [server side] ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    Is it possible to use WideCharToMultiByte with Letterlike Symbols [like ""]?

    Help!

    I need to send and 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, Im using WideCharToMultiByte func []Content not found.

    However, Im 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 Im 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

    WTF is going on? What am I doing wrong? Any guess?

    Is it possible to use WideCharToMultiByte with Letterlike Symbols?

    Many thanks,

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,670
    You shouldn't put extended character literals directly into your source code. The value can change depending on how source editor saves the file.
    Code:
    #include <windows.h>
    #include <string>
    #include <vector>
    #include <fstream>
    #include <iostream>
    
    std::string wstr_to_str(const std::wstring &wstr, UINT cp = CP_ACP)
    {
        int len = WideCharToMultiByte(cp, 0, wstr.c_str(), (int)wstr.length(), 
                                      0, 0, 0, 0);
        if (!len)
            return "ErrorA";
    
        std::vector<char> abuff(len + 1);
    
        // NOTE: this does not NULL terminate the string in abuff, but this is ok
        //       since it was zero-initialized in the vector constructor
        if (!WideCharToMultiByte(cp, 0, wstr.c_str(), (int)wstr.length(), 
                                 &abuff[0], len, 0, 0))
        {
            return "ErrorA";
        }//if
    
        return &abuff[0];
    }//wstr_to_str
    
    using namespace std;
    
    int main()
    {
        const wchar_t TM = 0x2122;
        wstring wide;
    
        wide = L"Skype ";
        wide += TM;
        wide += L"4.2.2";
    
        string utf8 = wstr_to_str(wide, CP_UTF8);
    
        ofstream f("test.text", ios::out | ios::binary);
        if (!f)
        {
            cerr << "Failed to open file" << endl;
            return 1;
        }//if
    
        f.write("\xEF\xBB\xBF", 3); // UTF8 BOM
        f.write(utf8.c_str(), utf8.length());
        f.close();
    
        return 0;
    }//main
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux Putting my children to sleep with futex!?
    By Abs in forum Linux Programming
    Replies: 18
    Last Post: 02-12-2009, 05:43 PM
  2. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  3. Trouble with Windows/DirectX programming
    By bobbelPoP in forum Windows Programming
    Replies: 16
    Last Post: 07-08-2008, 02:27 AM
  4. Strange error?
    By MrLucky in forum C++ Programming
    Replies: 5
    Last Post: 02-04-2006, 02:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21