I need to send string data with special characters from a Symbian S60 3rd Ed. phone to a server running on a Windows platform. The server is written in C++/CLI and I need the string data as a managed string on the server. I currently convert the string to UTF8 on the phone and send it, like this:
The descriptor aMessage of course contains the message to be sent. Then, on the server I convert it back using the following code:
HBufC* heapbuffer = HBufC::NewLC(aMessage.Length());
heapbuffer->Des().Copy( aMessage );
HBufC8* heapbuffer8 = HBufC8::NewLC(aMessage.Length());
This works, most of the time. However, sometimes the data seems to be sent incorrectly. For example, I had big problems sending over a string containing the number 26. When appending this number to another string to be passed as an SQL statement to a function, the server crashed. However, if I first convert the string to a number containing the number 26, and the convert it back to a string it works.
Encoding^ enc = Encoding::UTF8;
int socket = (int)param, bytesReceived, selectret;
bytesReceived = recv(socket, cbuf, BUFFER_MAX_LENGHT, 0);
cbuf[bytesReceived] = '\0';
array<Byte>^ receivedBytes = gcnew array<Byte>(bytesReceived + 1);
for(int i = 0; i < (bytesReceived + 1); i++)
receivedBytes[i] = cbuf[i];
System::String^ managedBuffer = enc->GetString(receivedBytes);
Any idea what is wrong with my string conversion? In the end I am simple trying to send data possibly containing special chars to a server running on .NET. How should I do that?