Hi there,
I was using an array of chars to take chars from a serial port buffer. But, when I do a cast to a string, for some strange reason, my string becomes something like string="haskjdh
Without the last " !!
I think this is really strange, did it happen to any of you
P.S. Now I use the ReadExisting method that returns a string directly, but I really curious about the reason for this strange error :-)
Thx
Code:
Dim buf(100) As Char
If (Me.SerialPort1.BytesToRead > 0 And Me.SerialPort1.BytesToRead > 100) Then
Me.SerialPort1.Read(buf, 0, 100)
ElseIf (Me.SerialPort1.BytesToRead > 0) Then
Me.SerialPort1.Read(buf, 0, Me.SerialPort1.BytesToRead)
End If
dim st as string=buf

String without terminator "
ronnycopeh
Hi,
The content of myString is "haskjgh
in the 1st example and "haskjgh" in the 2nd example.
NOT ""haskjgh" as you say.
Note you can not do this with any string in VB.>>
Dim myString As String
myString = haskjgh
it HAS to be within quotes, that is what i am meaning.
Regards,
S_DS
Mitch Walker - MSFT
Hi,
Ignore what you see in the code, it's what it prints as ( as you put it ) is what the string contains.
A string like any other variable has a box in memory, it doesn't need the double quotations marks or the > "" < actually in the string itself, they are known as delimiters or markers for what is in the box i.e you want the following in theBox.>> a "H" followed by a "e" then "l" then l" then an "o"
Dim theBox As String = "Hello"
Understand now
I may have confused you by adding CHR(34) the code for a > " < into the string.
The watch is the string name so ignore that as you are concerned with the contents.
The contents is what it wll print as.
ThE_lOtUs mentioned ascii character zero.
This would be Chr(0)
See http://www.asciitable.com/ for the printable characters.
My definition of STRING: ANY mixture of printable and non-printable characters such as>>
This is a string
"This is also a string"......................including the dots and these words.
Your confusion is thinking a string must have an opening " and a closing ".
When writing code that last sentence is true for Basic, VB and VB.Net and some other ( but not all ) programming languages.
I dont think VB.Net will let you write>>
Dim myString As String = " " Hello. " "
Hope you see now what i mean.
Regards,
S_DS
MunishGupta
Thanks for explaining, I already knew that the string doesn't have "" saved in memory, the thing was sth like syntax of the language to represent strings having this terminators "".
I see that I was not explaining myself very clear, but I believe this could be sth to do with the Chr(0), I remember when I was programming in assembly that we had to put it in the end, or else the program would keep on reading the remaining bytes in memory. It just seems a little stupid that VB doesnt take care of this problem, it should be sth that could not be possible, to "fill" the string with characters without the last byte Chr(0).
Oh, well, what to do, just go around and use another method :-)
Thx both of you
Jeff F
The string without the ", i see it while debugging.
In your example is myString= ""haskjgh" , in my case is "haskjgh and I cannot print it or add more characters.
RD410
Or I'm not understanding what you're saying or I'm not making myself understood.
What I'm saying is that when I add a watch or evaluate your 1st myString I see ""haskjgh" and it's printed "haskjgh. In the 2nd one I see ""haskjgh"" and It's printed "haskjgh".
In my program if I had the same characters as you coming from the serial buffer "h" & "a" & "s" & .... and when I did:
dim myString as string = buffer the watch of my string was: "haskjgh and the print was: haskjgh
The problem started because I was saving that string and adding new data next time I had data received. In this case it didn't let me add more since my string was not in the format "hasjgh" (as all strings I had seen until this time ) but just "hasjgh
To sum up: The problem is not extra ", the problem is that this string was in a format that I've never seen before, it was not "sth sth sth" but "sth sth sth
It looks like a visualStudio bug more than a problem of my code, it's like if i was doing sth like this: dim myString as string = "sth sth
Visual Studio doesnt even let me, it puts the last " automatically :-)
Anyway, thx a lot for the concern
maddman
I never done thing like that in .NET but in C/C++ the last character of a string had to be the ascii 0 (not the number 0 in string). This character (or byte) indicated the end of a string. Maybe this is what happened.
This was the cause of some buffer overflow. If you had an array of 4 character and the last one was not a 0, then when you wanted to print the string it would also go into random peice of memory.
hrubesh
Dim MyString as String = InputString.Trim("")
&#42;Jinx
Hi,
It looks to me like your receive buffer is getting ascii character 34 too.
See >> http://www.asciitable.com/ for details.
like writing.>>
Dim myString As String
myString = CHR(34) & "haskjdh"
MsgBox(myString)
' This second MsgBox will show you the difference.>>
MsgBox(myString &CHR(34))
The " character which is ascii 34 is a container character for a string in most but not all programming or script programs.
Some use a single quote, i.e. the ' character, as in SQL.
Vb or VB.net
"Hello world!!"
SQL
'Hello world!!'
Hope this helps.
Regards,
S_DS