473,472 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

Jan 26 '07 #1
3 13800
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
3
by: Steve Mauldin | last post by:
I came across an example in the MSDN documentation using RC2 encryption(the link to the article is at the end of this message). When I tried it I had a problem with getting back the same length...
3
by: craig.wagner | last post by:
I've got an application that is calling a command-line executable. The command-line tool uses stdin and stdout as its interface, and it expects a binary stream in and sends a binary stream out. ...
3
by: TJO | last post by:
Can someone recomend a good technique for removing BOF and EOF markers from a byte? I am passing a byte thru an FTP socket. I am serializing an object into a byte and then adding an EOF marker...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
13
by: Angus | last post by:
Hello I have a stream of bytes - unsigned char*. But the 'string' may contain embedded nulls. So not like a traditional c string terminated with a null. I need to calculate the length of...
1
by: =?Utf-8?B?ai5hLiBoYXJyaW1hbg==?= | last post by:
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,...
12
by: steven acer | last post by:
hello, i have a java app that constructs an xml from a specific file format and vice versa. i've been asked to convert it to c++, but im not an expert in c++, actually im mere beginner you can...
2
by: =?Utf-8?B?QWJoaW1hbnl1IFNpcm9oaQ==?= | last post by:
Hi, I am using Visual C++ in Visual Studio 2005 to create a Managed Wrapper around some C++ LIBS. I've created some classes that contains a pointer to the LIB classes and everthing seems to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.