473,513 Members | 2,339 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem to remove \0 in the array in the best way

Hi,

I have a windows C# app which read data through socket from a C++ app in a
linux box. After a marshal a pointer and get an array of string ends with \0,
I did something like this

string s=new string(array);
s = s.Replace("\0","");
if(s == "ABC")
success
else
fail

However, if I did it in my test all in C#, it works. If I plug into the
production and start to listen to the linux box messages, it always fail. If
Studio's output window, I can see s becomes ABC~ instead of ABC.

Does anyone here know what is the most efficient way to solve this problem?
I do can iterate the array to detect \0, but I do want the most efficient way
since I am facing a very heavy traffic from that linux box.

Thanks a lot

Chris

Nov 17 '05 #1
6 1828
chrisben <ch******@discussions.microsoft.com> wrote:
I have a windows C# app which read data through socket from a C++ app in a
linux box. After a marshal a pointer and get an array of string ends with \0,
I did something like this

string s=new string(array);
s = s.Replace("\0","");
if(s == "ABC")
success
else
fail

However, if I did it in my test all in C#, it works. If I plug into the
production and start to listen to the linux box messages, it always fail. If
Studio's output window, I can see s becomes ABC~ instead of ABC.

Does anyone here know what is the most efficient way to solve this problem?
I do can iterate the array to detect \0, but I do want the most efficient way
since I am facing a very heavy traffic from that linux box.


Well, your code is definitely removing \0 characters, so the rogue
character in s is something different. I suggest you print out its
Unicode value.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
chrisben <ch******@discussions.microsoft.com> wrote:
I have a windows C# app which read data through socket from a C++ app in a
linux box. After a marshal a pointer and get an array of string ends with \0,
I did something like this

string s=new string(array);
s = s.Replace("\0","");
if(s == "ABC")
success
else
fail

However, if I did it in my test all in C#, it works. If I plug into the
production and start to listen to the linux box messages, it always fail. If
Studio's output window, I can see s becomes ABC~ instead of ABC.

Does anyone here know what is the most efficient way to solve this problem?
I do can iterate the array to detect \0, but I do want the most efficient way
since I am facing a very heavy traffic from that linux box.


Well, your code is definitely removing \0 characters, so the rogue
character in s is something different. I suggest you print out its
Unicode value.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
Thanks. Actually, I checked the input again and realize there is more than /0
in the input. That is a 5 byte array, after converting to char array, it
shows as below in debu window

{Length=5}
[0]: 65 'A'
[1]: 73 'B'
[2]: 89 'C'
[3]: 0 ''
[4]: 732 '˜'
Any idea about what is going on?

Thanks

"Jon Skeet [C# MVP]" wrote:
chrisben <ch******@discussions.microsoft.com> wrote:
I have a windows C# app which read data through socket from a C++ app in a
linux box. After a marshal a pointer and get an array of string ends with \0,
I did something like this

string s=new string(array);
s = s.Replace("\0","");
if(s == "ABC")
success
else
fail

However, if I did it in my test all in C#, it works. If I plug into the
production and start to listen to the linux box messages, it always fail. If
Studio's output window, I can see s becomes ABC~ instead of ABC.

Does anyone here know what is the most efficient way to solve this problem?
I do can iterate the array to detect \0, but I do want the most efficient way
since I am facing a very heavy traffic from that linux box.


Well, your code is definitely removing \0 characters, so the rogue
character in s is something different. I suggest you print out its
Unicode value.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #4
Thanks. Actually, I checked the input again and realize there is more than /0
in the input. That is a 5 byte array, after converting to char array, it
shows as below in debu window

{Length=5}
[0]: 65 'A'
[1]: 73 'B'
[2]: 89 'C'
[3]: 0 ''
[4]: 732 '˜'
Any idea about what is going on?

Thanks

"Jon Skeet [C# MVP]" wrote:
chrisben <ch******@discussions.microsoft.com> wrote:
I have a windows C# app which read data through socket from a C++ app in a
linux box. After a marshal a pointer and get an array of string ends with \0,
I did something like this

string s=new string(array);
s = s.Replace("\0","");
if(s == "ABC")
success
else
fail

However, if I did it in my test all in C#, it works. If I plug into the
production and start to listen to the linux box messages, it always fail. If
Studio's output window, I can see s becomes ABC~ instead of ABC.

Does anyone here know what is the most efficient way to solve this problem?
I do can iterate the array to detect \0, but I do want the most efficient way
since I am facing a very heavy traffic from that linux box.


Well, your code is definitely removing \0 characters, so the rogue
character in s is something different. I suggest you print out its
Unicode value.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5
chrisben <ch******@discussions.microsoft.com> wrote:
Thanks. Actually, I checked the input again and realize there is more than /0
in the input. That is a 5 byte array, after converting to char array, it
shows as below in debu window

{Length=5}
[0]: 65 'A'
[1]: 73 'B'
[2]: 89 'C'
[3]: 0 ''
[4]: 732 '?'
Any idea about what is going on?


It looks like you're potentially using the wrong encoding to convert
the data into characters, and also potentially you're not decoding the
right amount of data in the first place.

What does the code you're using to read from the socket look like?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
chrisben <ch******@discussions.microsoft.com> wrote:
Thanks. Actually, I checked the input again and realize there is more than /0
in the input. That is a 5 byte array, after converting to char array, it
shows as below in debu window

{Length=5}
[0]: 65 'A'
[1]: 73 'B'
[2]: 89 'C'
[3]: 0 ''
[4]: 732 '?'
Any idea about what is going on?


It looks like you're potentially using the wrong encoding to convert
the data into characters, and also potentially you're not decoding the
right amount of data in the first place.

What does the code you're using to read from the socket look like?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

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

Similar topics

4
3455
by: John Broderick | last post by:
I've got a problem with the RichTextBox control in .Net where the garbage collector doesn't reclaim its memory after I've finished with it. My program adds a series of rtb's to the control array...
8
8930
by: Timo | last post by:
I am trying to get address of myStruct to a string array texts. I am using M$ Visual C++ 6.0 (this is not OS specific question, though, this code should also work on 16 bit embedded compiler ;)). ...
2
9906
by: Chris Plowman | last post by:
Hi all, I was wondering if anyone can help me with a really annoying problem I have been having. I made a derived datagrid class that will select the row when a user clicks anywhere on a cell...
1
3878
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
39
19561
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
19
6295
by: brasilino | last post by:
Hi Folks: I've been looking (aka googling) around with no success. I need a usability beyond 'pop()' method when removing an Array elements. For example: oName = new...
1
2045
by: DJG79 | last post by:
Hi all, I am using an open source menu that i found and it works great, except for one thing that when the web page is not scrolled to the very top the drop down links will not stay visible. Has...
12
1779
by: David | last post by:
Hi, I am trying to achieve the following: 1/ To have a standard form in an asp web page which has various check boxes and radio buttons. As a user selects a form item it updates a text box,...
0
1954
by: Filemaxor | last post by:
I have gotten my code to be able to allow people to add new text to a .txt document and able to call up files so the user can see whats in it. The problem i'm having is getting the for loop to work...
0
7267
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
7553
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...
1
7120
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...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5697
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5100
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
4754
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
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
466
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.