0

I am developing a C# Server and a C++ Client. The C++ client sends some unicode strings to the server. This is OK. I have a string object and use the data() function to get the byte array and send it over the socket to the c# server. This is all ok. The server receives the message correctly. The problem is with the receiving. I receive the bytes from the c# server however I want a way to "convert" the bytes to the corresponding unicode string.

I tried to create a wide string object:

wstring str = wstring((wchar_t *) buff);

and extract the string via the c_str() function but the result string is not the string the server sends!!

Where the buff is a byte array(unsigned char array) which I receive from the socket.

Any help would be appreciated!!!

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
user969245
  • 83
  • 1
  • 1
  • 8

1 Answers1

3

Here's how I would go about trying to solve this problem:

  1. Start with the server. Check the data to be sent. Is it what you expect?
  2. Now the connection. Run Wireshark and check the actual data sent over the Ethernet. Is it what you expect?
  3. On the client end, check the buffer. Is it what you expect?
  4. On the client end, check the wstring. Is it what you expect?

Start with a known entity (data being sent from the server) and check it every step of the way to see where it stops being what you expect it to be. This will tell you where your error is. Once you know that, if you are still having problems, post detailed information about the spot with the error.

Joel Rondeau
  • 7,486
  • 2
  • 42
  • 54
  • I can confirm that the bytes sent are correct. The problem is with the 4th step. The wstring... – user969245 Jan 02 '12 at 21:34
  • In other words what i want to do is implement the corresponding UTF8.GetString() c# .net function – user969245 Jan 02 '12 at 22:42
  • I'm confused. You said above that both are UTF-16. If that's the case, you don't need a UTF8.GetString(). You should just be able to cast the UTF-16 data array and be good. If you're actually transferring the data as UTF8, then you would need to do a conversion. Can see this question: http://stackoverflow.com/questions/148403/utf8-to-from-wide-char-conversion-in-stl – Joel Rondeau Jan 03 '12 at 16:42