I\'m trying to send a string containing special characters through a TcpClient (byte[]). Here\'s an example:
Your problem appears to be the Encoding.ASCII.GetBytes("amé");
and Encoding.ASCII.GetString(buffer);
calls, as hinted at by '500 - Internal Server Error' in his comments.
The é
character is a multi-byte character which is encoded in UTF-8 with the byte sequence C3 A9
. When you use the Encoding.ASCII
class to encode and decode, the é
character is converted to a question mark since it does not have a direct ASCII encoding. This is true of any character that has no direct coding in ASCII.
Change your code to use Encoding.UTF8.GetBytes()
and Encoding.UTF8.GetString()
and it should work for you.
Never too late to answer a question I think, hope someone will find answers here.
C# uses 16 bit chars, and ASCII truncates them to 8 bit, to fit in a byte. After some research, I found UTF-8 to be the best encoding for special characters.
//data to send via TCP or any stream/file
byte[] string_to_send = UTF8Encoding.UTF8.GetBytes("amé");
//when receiving, pass the array in this to get the string back
string received_string = UTF8Encoding.UTF8.GetString(message_to_send);
Your question and your error is not clear to me but using Base64String
may solve the problem
Something like this
static public string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes
= System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue
= System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
static public string DecodeFrom64(string encodedData)
{
byte[] encodedDataAsBytes
= System.Convert.FromBase64String(encodedData);
string returnValue =
System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
return returnValue;
}