469,347 Members | 17,866 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,347 developers. It's quick & easy.

Convert Byte Array to String

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
3 2562

<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
Someone in our local .NET User Group responded with a simple solution
that worked like a charm.

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

Nov 9 '05 #3

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.

Similar topics

1 post views Thread by Swarup | last post: by
6 posts views Thread by Ricardo Quintanilla | last post: by
15 posts views Thread by Kueishiong Tu | last post: by
6 posts views Thread by Allan Ebdrup | last post: by
6 posts views Thread by Bob Altman | last post: by
12 posts views Thread by Peter | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.