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?
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?