views:

271

answers:

5

I'm using this code to return some string from a tcpclient but when the string comes back it has a leading " character in it. I'm trying to remove it but the Len() function is reading the number of bytes instead of the string itself. How can I alter this to give me the length of the string as I would normally use it and not of the array underlying the string itself?

 Dim bytes(tcpClient.ReceiveBufferSize) As Byte
 networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

 ' Output the data received from the host to the console.'
 Dim returndata As String = Encoding.ASCII.GetString(bytes)

 Dim LL As Int32 = Len(returndata)

Len() reports the number of bytes not the number of characters in the string.

A: 

You could try trimming the string inside the call to Len():

Dim LL As Int32 = Len(returndata.Trim())
ichiban
does't work, I just tried that.
Middletone
A: 

If Len reports the number of bytes and it doesn't match the number of characters, then I can think of two possibilities:

  • There are more chars being sent than you think (ie, that extra character is actually being sent)

  • The encoding is not ASCII, so there can be more than one byte per char (and one of them is that 'weird' character, that is the character is being sent and is not 'wrong data'). Try to find out if the data is really ASCII encoded, if not, change the call accordingly.

Vinko Vrsalovic
A: 

When I read you correctly, you get a single quotation mark at the beginning, right?

If you get that one consistently why not just subtract one from the string length? Or use a substring from the second character:

Len(returndata.Substring(1)

And I don't quite understand what you mean with »the length of the string as I would normally use it and not of the array underlying the string itself«. You have a string. Any array which might represent that string internally is entirely implementation-dependent and nothing you should see or rely on. Or am I getting you wrong here. The string is what you are using normally. I mean, if that's not what you do, then why not take the length of the string after processing it into something you would normally use?

Joey
A: 

Maybe I am missing something here, but what is wrong with String.Length?

Dim LL As Int32 = returndata.Length
Fredrik Mörk
Nothing, but it is exactly equivalent to Len
MarkJ
+5  A: 

Your code is currently somewhat broken. The answer is tcpClient.ReceiveBufferSize, regardless of how much data you actually received - because you're ignoring the return value from networkStream.Read. It could be returning just a few bytes, but you're creating a string using the rest of the bytes array anyway. Always check the return value of Stream.Read, because otherwise you don't know how much data has actually been read. You should do something like:

Dim bytesRead = networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

' Output the data received from the host to the console.'
Dim returndata As String = Encoding.ASCII.GetString(bytes, 0, bytesRead)

Now, ASCII always has a single character per byte (and vice versa) so the length of the string will be exactly the same as the length of the data you received.

Be aware that any non-ASCII data (i.e. any bytes over 127) will be converted to '?' by Encoding.ASCII.GetString. You may also get control characters. Is this definitely ASCII text data to start with? If it's not, I'd recommend hex-encoding it or using some other option to dump the exact data in a non-lossy way.

Jon Skeet