473,385 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1825
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
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
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
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
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
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
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
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
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
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.