marvinc
marvinc

Reputation: 61

VC++ decoding diacritics characters from stream

I have a small application client/server app. Client app(C#), server side(VC++). My application will send any character to server side using streams; english characters works fine except non-english. VC++ is using CString to accept this data from stream. I fix some of diacritics characters to read correctly when received but some of it won't work. Client side encoded this characters using UnicodeEncoding() before sending it. It works fine on characters É, À, È, Ù, Â, Ê, Î, Ô, Û, Ë, Ï, Ö, Ü except on Ÿ(this one will read as x_ on server side) and Turkish character are read incorrectly.

Questions:

A sample code would be a great help.

Thanks.

Upvotes: 0

Views: 105

Answers (2)

marvinc
marvinc

Reputation: 61

In case someone stumble with the same problem.

Here's how I resolve my problem. On client side

if (IsUnicode(str))
{
   writer.Write((uint)str.Length + 1);
   writer.Write(Encoding.Unicode.GetBytes(str));
}

private bool IsUnicode(string text)
    {
        if (string.IsNullOrEmpty(text))
            return false;

        foreach (char c in text)
        {
            if (c > 0x7F)
                return true;
        }
        return false;
    }

On server side Convert the string using CW2A with CP_UTF8 parameter

CStringA str = CW2A(m_CString, CP_UTF8);
m_CString.Format(_T("%s"), str);            

Upvotes: 0

jdweng
jdweng

Reputation: 34433

The code below works. So the data is being changed or somewhere a buffer isn't being flushed. My usual recommendations is to check the byte count and see where the byte count is being changed. Usually it is a stream class that isn't being set to unicode.

            string input = "ÉÀÈÙÂÊÎÔÛËÏÖÜŸ";
            byte[] temp = Encoding.Unicode.GetBytes(input);
            string output = Encoding.Unicode.GetString(temp);

Upvotes: 1

Related Questions