Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C# Programming

Converting byte[] to string - removing NULLs??


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 <jeffrey_no_spam_al@nospam.nospam> wrote:
> 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 - <s@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

-----------------------------------------------Reply-----------------------------------------------

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 <jeffrey_no_spam_al@nospam.nospam> wrote:
> 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 - <s@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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc