C# Converting socket communitation ( byte[] ) to string
-
Hi, Sorry if this is a simple question, I'm learning c# and I'm trying to send a string to another computer, but when I receive the string I want to store that string in an xml file the problem is that the string comes with "empty" characters at the end. so my xml looks like "abc.........) follow by many empty chars. here is the code I'm using to get the data. using tcp sockets
Byte\[\] received = new Byte\[256\]; int bytesReceived = server1Tcp.Receive(input, input.Length, 0); dataReceived = System.Text.Encoding.ASCII.GetString(input);
How can I save the "dataReceived" string without the empty chars. again, thank you to all of you who have helped me learn C# -JC
-
Hi, Sorry if this is a simple question, I'm learning c# and I'm trying to send a string to another computer, but when I receive the string I want to store that string in an xml file the problem is that the string comes with "empty" characters at the end. so my xml looks like "abc.........) follow by many empty chars. here is the code I'm using to get the data. using tcp sockets
Byte\[\] received = new Byte\[256\]; int bytesReceived = server1Tcp.Receive(input, input.Length, 0); dataReceived = System.Text.Encoding.ASCII.GetString(input);
How can I save the "dataReceived" string without the empty chars. again, thank you to all of you who have helped me learn C# -JC
Add this at below the posted code
dataReceived.Trim();
-
Hi, Sorry if this is a simple question, I'm learning c# and I'm trying to send a string to another computer, but when I receive the string I want to store that string in an xml file the problem is that the string comes with "empty" characters at the end. so my xml looks like "abc.........) follow by many empty chars. here is the code I'm using to get the data. using tcp sockets
Byte\[\] received = new Byte\[256\]; int bytesReceived = server1Tcp.Receive(input, input.Length, 0); dataReceived = System.Text.Encoding.ASCII.GetString(input);
How can I save the "dataReceived" string without the empty chars. again, thank you to all of you who have helped me learn C# -JC
Trim the input, IIRC you get the full buffer length in string.
-
Hi, Sorry if this is a simple question, I'm learning c# and I'm trying to send a string to another computer, but when I receive the string I want to store that string in an xml file the problem is that the string comes with "empty" characters at the end. so my xml looks like "abc.........) follow by many empty chars. here is the code I'm using to get the data. using tcp sockets
Byte\[\] received = new Byte\[256\]; int bytesReceived = server1Tcp.Receive(input, input.Length, 0); dataReceived = System.Text.Encoding.ASCII.GetString(input);
How can I save the "dataReceived" string without the empty chars. again, thank you to all of you who have helped me learn C# -JC
The way I would do it is to send the length of the string, and then the string itself. So you would have something like:
byte[] myBytes = ...;
//Read the first four bytes and get an int
int length = BitConverter.ToInt32(myBytes, 0);
//Start at the 4th byte (after the int) and read until the end of the string
string myString = System.Text.Encoding.ASCII.GetString(myBytes, 4, length);My current favourite word is: I'm starting to run out of fav. words!
-SK Genius
-
Hi, Sorry if this is a simple question, I'm learning c# and I'm trying to send a string to another computer, but when I receive the string I want to store that string in an xml file the problem is that the string comes with "empty" characters at the end. so my xml looks like "abc.........) follow by many empty chars. here is the code I'm using to get the data. using tcp sockets
Byte\[\] received = new Byte\[256\]; int bytesReceived = server1Tcp.Receive(input, input.Length, 0); dataReceived = System.Text.Encoding.ASCII.GetString(input);
How can I save the "dataReceived" string without the empty chars. again, thank you to all of you who have helped me learn C# -JC
nyjcr wrote:
the problem is that the string comes with "empty" characters at the end.
No, the string doesn't come with any empty characters. The problem is that you get the length of the actual data, but then you just ignore that and convert the entire buffer into a string. The unused part of the buffer just happens to be filled with zeroes. Only convert the actual data:
dataReceived = System.Text.Encoding.ASCII.GetString(input, 0, bytesReceived);
Despite everything, the person most likely to be fooling you next is yourself.