473,835 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about strings & encoding

when I execute

dim strTEMP_DATA as string
strTEMP_DATA = Encoding.ASCII. GetString(rBuff er).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 1533
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.super news.com... when I execute

dim strTEMP_DATA as string
strTEMP_DATA = Encoding.ASCII. GetString(rBuff er).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(rBuffer Size) As Byte
Private Const rBufferSize As Integer = 1024

Private Sub LocalDataArriva lEvent(ByVal ar As IAsyncResult)
Dim strTEMP_DATA As String
Dim strBDR As System.Text.Str ingBuilder
Try
Dim read As Integer = lSocket.EndRece ive(ar)
If read > 0 Then
strTEMP_DATA = Encoding.ASCII. GetString(rBuff er)
RaiseEvent DataArrival(str TEMP_DATA)
rBuffer.Clear(r Buffer, 0, rBufferSize)
lSocket.BeginRe ceive(rBuffer, 0, rBufferSize, SocketFlags.Non e, New
AsyncCallback(A ddressOf Me.LocalDataArr ivalEvent), 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******** *****@tk2msftng p13.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.super news.com...
when I execute

dim strTEMP_DATA as string
strTEMP_DATA = Encoding.ASCII. GetString(rBuff er).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(Encod ing.ASCII.GetSt ring(rBuffer))
and everything works now.

"Martin Fletcher" <Ma************ *@msn.com> wrote in message
news:vr******** ****@corp.super news.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(rBuffer Size) As Byte
Private Const rBufferSize As Integer = 1024

Private Sub LocalDataArriva lEvent(ByVal ar As IAsyncResult)
Dim strTEMP_DATA As String
Dim strBDR As System.Text.Str ingBuilder
Try
Dim read As Integer = lSocket.EndRece ive(ar)
If read > 0 Then
strTEMP_DATA = Encoding.ASCII. GetString(rBuff er)
RaiseEvent DataArrival(str TEMP_DATA)
rBuffer.Clear(r Buffer, 0, rBufferSize)
lSocket.BeginRe ceive(rBuffer, 0, rBufferSize, SocketFlags.Non e, New
AsyncCallback(A ddressOf Me.LocalDataArr ivalEvent), 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******** *****@tk2msftng p13.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.super news.com...
when I execute

dim strTEMP_DATA as string
strTEMP_DATA = Encoding.ASCII. GetString(rBuff er).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(rBuff er)
I would use:
strTEMP_DATA = Encoding.ASCII. GetString(rBuff er, 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.super news.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(rBuffer Size) As Byte
Private Const rBufferSize As Integer = 1024

Private Sub LocalDataArriva lEvent(ByVal ar As IAsyncResult)
Dim strTEMP_DATA As String
Dim strBDR As System.Text.Str ingBuilder
Try
Dim read As Integer = lSocket.EndRece ive(ar)
If read > 0 Then
strTEMP_DATA = Encoding.ASCII. GetString(rBuff er)
RaiseEvent DataArrival(str TEMP_DATA)
rBuffer.Clear(r Buffer, 0, rBufferSize)
lSocket.BeginRe ceive(rBuffer, 0, rBufferSize, SocketFlags.Non e, New
AsyncCallback(A ddressOf Me.LocalDataArr ivalEvent), 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******** *****@tk2msftng p13.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.super news.com...
when I execute

dim strTEMP_DATA as string
strTEMP_DATA = Encoding.ASCII. GetString(rBuff er).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
2262
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 obscure code ranges?
9
8582
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 reference (i.e.  ) and it seems to be converting the character to its UNICODE representation. Take this source XML document: <?xml version="1.0" encoding="UTF-8"?>
2
4360
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 href="identification.php?PHPSESSID=134134&page=2&param=3" > </form> what I want:
3
5708
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 to convert XML like this: <?xml version="1.0" encoding="UTF-8"?> <java version="1.4.2_03" class="java.beans.XMLDecoder"> <object class="javax.swing.JButton"> <string>Hello, world</string>
4
6076
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 script that grabs some web pages from the web, regex parse the data and stores it localy to xml file for further use.. at first i had no problem using python minidom and everything concerning
2
1806
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" (the backward R) and it came out as "Я". So far so good. Then I sent in " Я" (The is the Cyrillic character again.) That came out as "Я Я". How can I please tell the difference between the Cyrillic and the character sequence '&', '#',
8
2820
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);" TextBox2.Attributes.Add("onKeyPress", jscode) You will notice that jscode contains the JavaScript Logical And operator (&&). However, ASP.NET renders this as &amp;&amp; in the code that is
1
1445
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 converse would sometimes be useful too, i.e. convert/treat string as byte array. Thanks
3
5354
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 "&" become "&amp;" and "<" becomes "<". This is a problem because I am trying to actually write out html. Does anyone know how I can solve this problem. For example
0
9803
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9652
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10523
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10560
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7766
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6966
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4434
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 we have to send another system
2
3993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.