|
If you trying to convert a byte array to string, stopping on the first null, then try:
byte[] ab = { 0x41, 0x42, 0x43, 0, 0x44, 0x45 };
StringBuilder sb = new StringBuilder(ab.Length);
foreach (byte b in ab)
{
if (b == 0)
{
break;
}
sb.Append((char) b);
}
string s = sb.ToString();
I don't know of a built-in way to do it (though I have been wrong before).
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
I thought Marshal.PtrToStringAnsi did that.. You have to give it a pointer though, so you need unsafe code.
|
|
|
|
|
I told you I could be wrong! Not too happy recommending unsafe code for this task, though - I prefer to keep it for when I need it (like converting strings to secure strings)
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
That just sounds ironic...needing "unsafe" code to create "secure" strings
Dybs
|
|
|
|
|
I know what you mean!
The problem is that managed (or safe) strings are duplicated, copied and may (or may not) be deleted - the unmanaged (or unsafe) stuff is more under your control, and thus "safer". I blame Microsoft for not introducing a "Secure" keyword for variables...
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
I probably wouldn't use it either (seems a bit like a canon to kill an ant, right?) but it exists
|
|
|
|
|
Hi,
GetString converts all the bytes from ASCII to Unicode, and 0 is a valid character, it is ASCII NULL.
If you know there is a NULL, then use ASCIIEncoding.ASCII.GetString(test, 0, test.Length-1);
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
string mystr = new string(Array.ConvertAll(test, x => (char) x));
xacc.ideIronScheme - 1.0 beta 3 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
|
IMO that is too much of a code to read. Are you trying to merge two files?
|
|
|
|
|
|
It would be very unusual for anybody to share an Email address with people they don't know.
You would help people to read your code if you put it between <pre></pre> tags, and format it properly, with indents and so on. Also only post relevant code, there is far too much in your question.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
|
As I said, format your code, cut out anything not directly related to the problem and then more people will be likely to read it and offer suggestions.
At the moment I am not prepared to strain my eyes and brain, trying to read your code. Therefore I am unable to help resolve your problem.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi Henry,
Thanks for your proposal. But I dont think it would be easier to understand for other people if I shorten it out. But I have another suggestion. If you can compile the code and then I can give you the example of security.txt and position.txt and then you may see the result and the problem itself.
Any thoughts?
|
|
|
|
|
I can't speak for Henry, but I would not compile and run source code from someone I didn't know well and hadn't examined thoughly - any more than I would run an EXE file I "just found" on a website.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
But if you just go through a bit you would never find that it is anything harmful. Actually I am new in this language and facing a tough problem wanted to share with others so that I can get the help. But I think my thinking or point of view was wrong.
Regards,
Sam
|
|
|
|
|
sammy15024 wrote: But if you just go through a bit you would never find that it is anything harmful
I think that is Henry's point - we don't want to go though all that stuff. You have to remember that we don't get paid for this - so we aren't going to put ourselves out too much unless we are really interested. Large amounts of unformatted, dull looking code with a "it doesn't allways work properly" comment are unlikely to be looked at.
A short, snappy formatted code fragment with "why does it ignore all lines with 'Banana' in them?" is a lot more likely to get a good response.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
You carry on being right and doing it your way.
I'll carry on not helping you, as, I suspect, will most others.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
It's ok Henry.
Thanks for your help and suggestion.
With regards,
Sam
|
|
|
|
|
sammy15024 wrote: . But I dont think it would be easier to understand for other people if I shorten it out.
Why do you think that ? you're dead wrong. If you actually try to debug it yourself, you'll either find the answer, or come up with a code snippet that you can intelligently explain what you expect, and what it is doing. What you're saying right now is, I wrote all this code, and I'm too dumb to understand it, so you do the work for me.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Hi Christian,
Thanks for your reply. I tried with all of my little knowledge. But was undone fully. Somewhere I am doing something silly so my exe is not working exactly. Since my code is dependant with other two files, I am not able to understand where there something is misiing in source files or in source code. That was the reason I posted all the source code.
Regards,
Sam
|
|
|
|
|
So, you stepped through the debugger, and all you can tell us is that something doesn't work in the source code as a whole ?
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
|
I'm sorry, but this is a disaster. This code is close to unreadable. It is full of idioms that make no sense. You don't define 'not working' and it's clear you've not bothered to use the debugger to work out why it's doing what it is. No-one can fix this code without running it, with the files you are using, and no-one is going to do that for you. We'd be happy to explain to you how to use the debugger, although you really should have a book to tell you that. This code is way too bad, and your skill level way too low, for you to be doing this for a paid job, so I assume you're doing this for a class. Your best bet, is to talk to your professor, so he/she knows how lost you are and is able to better help you going forward.
Of course, the industry in the state it's in, I am aware that someone may well be paying for this mess. If that's true, it's an absolute disgrace. Of course, if it's not, then everyone has to learn, the issue here is that instead of learning, you're asking us to fix it for you. The debugger is there for a reason, you will never cut it as a programer unless you take the time to learn how to use it.
But, if you're willing to at least try to diagnose your own problem, and if you have a specific question when you know exactly what is going wrong, I'm sure we'd be happy to help you. I would recommend reading the guidelines on how to ask questions before posting another one, you broke a fair number of them in your post this time.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|