I have problem with sending Cyrillic text "УБА221" via C# socket. It turned to "#xxxxx221". Here is xxxxx are 5 strange alphabets I can't copy paste.
Server side(receiving side):
SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
int iRx = theSockId.thisSocket.EndReceive(asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
TextLabel.Text =TextLabel.Text+ szData;
Client side(sending):
subtext = "УБА221";
byte[] byData = System.Text.Encoding.UTF8.GetBytes(subtext);
socket.Send(byData);
Is there any idea why my code is not working?
Look at how you're encoding the text to send it:
byte[] byData = System.Text.Encoding.UTF32.GetBytes(subtext);
Now look at how you're decoding the binary data back into text:
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
You're using UTF-32 to encode, and UTF-8 to decode. That's not going to work. You need to use the same encoding on both sides.
Also note:
BeginReceive/EndReceive
callEncoding.UTF8.GetString(bytes, 0, bytesToDecode)
rather than going via Decoder
.Decoder
, but you'll need to preserve the Decoder
between read calls, as that stores the state of "not yet decoded data".See more on this question at Stackoverflow