By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,467 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,467 IT Pros & Developers. It's quick & easy.

Converting byte[] to string - removing NULLs??

P: n/a
Hello,
I've eliminated the bulk of code, this should be sufficient:

byte[] fromEncrypt;
string sDecryptedString;

//Read the data out of the crypto stream.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

//At this point in the debugger, I can see there are "trailing" NULLs '\0'
in several of the last fromEncrypt array elements

//Convert the byte array back into a string.
sDecryptedString = textConverter.GetString(fromEncrypt);

I can see β€˜\0’ in a number of the array elements of fromEncrypt (at the end
of the array). When I put that into the sDecryptedString, the nulls are
still there.

Other than using a β€œfor” loop and parsing each element, is there a single
command that can remove the trailing nulls from either the fromEncrypt or the
sDecryptedString?

..Trim() or .Trim('\0') didn't work.

Thanks!! Jeff

Jan 26 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
j.a. harriman <je*******************@nospam.nospamwrote:
I've eliminated the bulk of code, this should be sufficient:
A short but *complete* program is always preferable, IMO.
byte[] fromEncrypt;
string sDecryptedString;

//Read the data out of the crypto stream.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
1) You are assuming that the decrypted data will be the same length as
the encrypted data. That may well not be true (and is almost ceratnily
the issue)

2) You are ignoring the return value of the Read method. Never do this.
See http://www.pobox.com/~skeet/csharp/readbinary.html
Other than using a ?for? loop and parsing each element, is there a single
command that can remove the trailing nulls from either the fromEncrypt or the
sDecryptedString?
Better than that is to not convert the bad data to start with - if you
fix the bugs listed above, I suspect you'll find you don't have
trailing nulls any more.
.Trim() or .Trim('\0') didn't work.
I bet they did, actually, but you probably didn't use the result. Note
that neither of them change the contents of the string you call them on
(as strings are immutable) - they return a *new* string which is the
result of the trimming.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 26 '07 #2

P: n/a
Thanks, Jon.

Your solution is certainly more thorough. The vlaue coming in is from a
known source and "should" always be the same.

I was able to trim the nulls at this point:
//Convert the byte array back into a string.
sDecryptedString =
textConverter.GetString(fromEncrypt).Trim('\0');

Thanks. Jeff

"Jon Skeet [C# MVP]" wrote:
j.a. harriman <je*******************@nospam.nospamwrote:
I've eliminated the bulk of code, this should be sufficient:

A short but *complete* program is always preferable, IMO.
byte[] fromEncrypt;
string sDecryptedString;

//Read the data out of the crypto stream.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

1) You are assuming that the decrypted data will be the same length as
the encrypted data. That may well not be true (and is almost ceratnily
the issue)

2) You are ignoring the return value of the Read method. Never do this.
See http://www.pobox.com/~skeet/csharp/readbinary.html
Other than using a ?for? loop and parsing each element, is there a single
command that can remove the trailing nulls from either the fromEncrypt or the
sDecryptedString?

Better than that is to not convert the bad data to start with - if you
fix the bugs listed above, I suspect you'll find you don't have
trailing nulls any more.
.Trim() or .Trim('\0') didn't work.

I bet they did, actually, but you probably didn't use the result. Note
that neither of them change the contents of the string you call them on
(as strings are immutable) - they return a *new* string which is the
result of the trimming.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 31 '07 #3

P: n/a
j.a. harriman <je*******************@nospam.nospamwrote:
Your solution is certainly more thorough. The vlaue coming in is from a
known source and "should" always be the same.
Why? Encryption *often* ends up with a different length of encrypted
data to decrypted data.
I was able to trim the nulls at this point:
//Convert the byte array back into a string.
sDecryptedString =
textConverter.GetString(fromEncrypt).Trim('\0');
I still *strongly* urge you to try to fix it "properly".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 31 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.