Converting byte[] to string - removing NULLs??

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
j.a. harriman said:
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.
 
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 said:
j.a. harriman said:
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.
 
j.a. harriman said:
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".
 
Back
Top