473,486 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Question about strings & encoding

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?
Nov 20 '05 #1
4 1521
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
"Martin Fletcher" <Ma*************@msn.com> wrote in message
news:vr************@corp.supernews.com... 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?

Nov 20 '05 #2
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:O7*************@tk2msftngp13.phx.gbl...
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
"Martin Fletcher" <Ma*************@msn.com> wrote in message
news:vr************@corp.supernews.com...
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?


Nov 20 '05 #3
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 Fletcher" <Ma*************@msn.com> wrote in message
news:vr************@corp.supernews.com...
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:O7*************@tk2msftngp13.phx.gbl...
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
"Martin Fletcher" <Ma*************@msn.com> wrote in message
news:vr************@corp.supernews.com...
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?



Nov 20 '05 #4
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

"Martin Fletcher" <Ma*************@msn.com> wrote in message
news:vr************@corp.supernews.com... 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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:O7*************@tk2msftngp13.phx.gbl...
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
"Martin Fletcher" <Ma*************@msn.com> wrote in message
news:vr************@corp.supernews.com...
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?



Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2243
by: Jonathon Blake | last post by:
All: Question Python is currently Unicode Compliant. What happens when strings are read in from text files that were created using GB 2312-1980, or KPS 9566-2003, or other, equally...
9
8523
by: Collin VanDyck | last post by:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem.. I am trying to get a Java/Xalan transform to pass through a numeric character...
2
4335
by: Sebek | last post by:
Hello, I'm transforming a XML document in XHTML but I have problems using sub-strings, it will be clearer with an exemple: What I have: <form...
3
5682
by: J Trost | last post by:
I was wondering if anyone knows if it is possible to do basic string replacement using XSLT even though the strings being replaced may contain "<" and ">". Here is my problem: I need to be able...
4
6035
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
2
1786
by: Louise GK | last post by:
Hi there. I've written a simple program that makes a simple GET form with a text input box and displays $_GET when submitted. Using Windows Character Map, I pasted in the Cyrillic capital "Ya"...
8
2773
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
1
1429
by: Steve Marshall | last post by:
Hi all, This is probably a real dumb question, but I just haven't come across the answer... Is there a simple way to treat a byte array as a string, or to convert it to a string? And the...
3
5322
by: =?Utf-8?B?cndvb2RydWY=?= | last post by:
Hello All, I am using an HtmlTextWriter to writer out some html. Prior to sending the content to the text writer, HttpUtility.HtmlEncode the string. However, doing so results in a string where...
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.