user969245
user969245

Reputation: 93

C++ Convert UTF Byte Array to String

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!!!

Upvotes: 1

Views: 2558

Answers (1)

Joel Rondeau
Joel Rondeau

Reputation: 7586

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.

Upvotes: 3

Related Questions