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

Convert Byte Array to String

P: n/a
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.

I have the calling side working and am capturing the return value into
a MemoryStream. Now I'm breaking up the MemoryStream into structures.

Some of the values coming back are character arrays which I want to
turn into strings. They are not length prefixed. Rather they are
fixed-length and null-padded. For example, the first bit of the return
DML looks like:

record
integer(4) ScannedBatchID;
ascii string(10) TestCode;
integer(1) RetestCode;

And so on...

I'm wrapping a BinaryReader around the MemoryStream and using the
appropriate Read* method to pull the values. For the strings I'm using
ReadChars and specifying the number of characters.

So here's my dilemma. If the string isn't full length, I get back a
string with trailing ASCII nulls (e.g. "ABCDEFGH\0\0"). I don't want
those trailing nulls in the string, so I created the following method.

private string GetNonTerminatedString( char [] chars )
{
string theString = String.Empty;

for( int i = 0; i < chars.Length; i++ )
{
if( chars[i] == '\x0000' )
{
theString = new string( chars, 0, i );
break;
}
}

return theString;
}

I'm wondering if anyone has encountered anything like this and has a
more efficient solution?

Nov 8 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

<cr**********@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
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.

I have the calling side working and am capturing the return value into
a MemoryStream. Now I'm breaking up the MemoryStream into structures.

Some of the values coming back are character arrays which I want to
turn into strings. They are not length prefixed. Rather they are
fixed-length and null-padded. For example, the first bit of the return
DML looks like:

record
integer(4) ScannedBatchID;
ascii string(10) TestCode;
integer(1) RetestCode;

And so on...

I'm wrapping a BinaryReader around the MemoryStream and using the
appropriate Read* method to pull the values. For the strings I'm using
ReadChars and specifying the number of characters.

So here's my dilemma. If the string isn't full length, I get back a
string with trailing ASCII nulls (e.g. "ABCDEFGH\0\0"). I don't want
those trailing nulls in the string, so I created the following method.

private string GetNonTerminatedString( char [] chars )
{
string theString = String.Empty;

for( int i = 0; i < chars.Length; i++ )
{
if( chars[i] == '\x0000' )
{
theString = new string( chars, 0, i );
break;
}
}

return theString;
}

I'm wondering if anyone has encountered anything like this and has a
more efficient solution?


Maybe using Encoding.Ascii to get teh string and then using Trim() to remove
the trailing nulls?
Nov 9 '05 #2

P: n/a
Someone in our local .NET User Group responded with a simple solution
that worked like a charm.

myString.Replace( "\0", "" );

Nov 9 '05 #3

P: n/a

Isn't String.TrimEnd method more appropriate?

craig.wag...@gmail.com wrote:
Someone in our local .NET User Group responded with a simple solution
that worked like a charm.

myString.Replace( "\0", "" );


Nov 9 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.