Convert Byte Array to String

C

craig.wagner

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 == '\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?
 
D

Daniel O'Connell [C# MVP]

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 == '\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?
 
C

craig.wagner

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top