Question about strings & encoding

  • Thread starter Thread starter Martin Fletcher
  • Start date Start date
M

Martin Fletcher

when I execute

dim strTEMP_DATA as string
strTEMP_DATA = Encoding.ASCII.GetString(rBuffer).ToString

I alway get a string without the closing quote. I see this in the 'Autos'
window, I also see it when I hoover my mouse over the strTEMP_DATA above.

An example would be:
It looks like "test
I believe it should look like "test"

Without the closing quote I CANNOT concat strings.
How can I correct this?
 
Martin,
It sounds like your string has an embedded null char in it (Char.MinValue).
Although it is perfectly valid for a String to contain a null char, the
Debugger treats it as a string terminator so it stops displaying it.
Without the closing quote I CANNOT concat strings.
The string is valid to .NET and you can concatenate other strings to it!
However with the null char, the debugger and System.Console and some for the
Windows Forms functions will not work as expected as they rely on Win32
calls which treat the null char as a string terminator.
How can I correct this?
What does your rBuffer look like, are you certain it only has valid ASCII
characters in it? Is the data in the buffer shorter then the length of the
buffer? In other woreds is there trailing zeros in the byte array?

Hope this helps
Jay
 
1st off, thanks for the fast reply.

I'm sure it only contains ASCII characters.
The data IS usualy shorter than the buffer length, so yes there are tailing
zeros in the byte array.

Is there a fix to get around this problem?


Here's some of the code I am having the problem with

Private rBuffer(rBufferSize) As Byte
Private Const rBufferSize As Integer = 1024

Private Sub LocalDataArrivalEvent(ByVal ar As IAsyncResult)
Dim strTEMP_DATA As String
Dim strBDR As System.Text.StringBuilder
Try
Dim read As Integer = lSocket.EndReceive(ar)
If read > 0 Then
strTEMP_DATA = Encoding.ASCII.GetString(rBuffer)
RaiseEvent DataArrival(strTEMP_DATA)
rBuffer.Clear(rBuffer, 0, rBufferSize)
lSocket.BeginReceive(rBuffer, 0, rBufferSize, SocketFlags.None, New
AsyncCallback(AddressOf Me.LocalDataArrivalEvent), lSocket)
Else
RaiseEvent DisConnect() 'peer closed connection
End If
Exit Sub
Catch
RaiseEvent DisConnect()
End Try
End Sub
 
I did a google and found this code:

Public Function StripNull(ByVal InString As String) As String
'Input: String containing null terminator (Chr(0))
'Returns: all character before the null terminator
Dim iNull As Integer
If Len(InString) > 0 Then
iNull = InStr(InString, vbNullChar)
Select Case iNull
Case 0
StripNull = InString
Case 1
StripNull = ""
Case Else
StripNull = Left$(InString, iNull - 1)
End Select
End If
End Function

In then code from my last post I changed it to look like
strTEMP_DATA = StripNull(Encoding.ASCII.GetString(rBuffer))
and everything works now.
 
Martin,
I would pass the length of the data in the buffer to the GetString method.

Instead of:
strTEMP_DATA = Encoding.ASCII.GetString(rBuffer)

I would use:
strTEMP_DATA = Encoding.ASCII.GetString(rBuffer, 0, read)

Note: you need to use the version that has starting index & length, hence
the zero in the above.

I would not use the StripNull sample you found on Google, I hope you will
agree based on the above StripNull is a lot of extra code that can be
avoided.

Hope this helps
Jay
 
Back
Top