|
hi,
i need to convert an ascii byte array to string.
in the byte [] could be 0 value that marks the end of the string.
i tried to do it like that :
byte[] test = { 0x41, 0x42, 0 };
string mystr = ASCIIEncoding.ASCII.GetString(test);
but the result is - "AB\0"
can i do it otherway to get just "AB" ?
thanks,
Samy
|
|
|
|
|
mystr.TrimEnd('\0');
Eslam Afifi
|
|
|
|
|
tried but doesn't work - still is the same. seems like the trim has no effect
|
|
|
|
|
mystr = mystr.TrimEnd('\0');
|
|
|
|
|
Of course it won't change the string it will return a new one. Strings are immutable. Sorry if the answer wasn't clear enoguh. PIEBALDconsult provided a full answer, and so did many others.
Eslam Afifi
|
|
|
|
|
This works (I tested it):
byte[] test = { 0x41, 0x42, 0 };
string mystr = ASCIIEncoding.ASCII.GetString(test);
if (mystr.EndsWith("\0"))
{
mystr = mystr.Substring(0, mystr.Length - 1);
}
Natza Mitzi
|
|
|
|
|
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.”
|
|
|
|