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? 3 2809
<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?
Someone in our local .NET User Group responded with a simple solution
that worked like a charm.
myString.Replace( "\0", "" );
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", "" ); This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Swarup |
last post by:
I am reading a file (txt, xml, gif, ico, bmp etc) byte by byte and filling
it into a byte arry. Now i have to convert it into a string to store it in
the database.
I use...
|
by: Ricardo Quintanilla |
last post by:
i have a code that sends data to a socket listening over as400 platform,
the socket responds to me as a "byte array".
then i need to convert the "byte array" into a string.
the problem is that...
|
by: Kueishiong Tu |
last post by:
How do I convert a Byte array (unsigned char managed) to a
char array(unmanaged) with wide character taken into
account?
|
by: Allan Ebdrup |
last post by:
How do I easily convert a int to a string?
Kind Regards,
Allan Ebdrup
|
by: moondaddy |
last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a
string, and then from a string back to a byte array.
for example
Private mByte() as New Byte(4){11,22,33,44}
Now how...
|
by: Bob Altman |
last post by:
Hi all,
I'm looking for the fastest way to convert an array of bytes to String. I
also need to convert a String back to its original Byte() representation.
Convert.ToBase64String and...
|
by: Peter |
last post by:
Trying to convert string to byte array.
the following code returns byte array of {107, 62, 194, 139, 64}
how can I convert this string to a byte array of {107, 62, 139, 65}
...
|
by: est |
last post by:
From python manual
str( )
Return a string containing a nicely printable representation of an
object. For strings, this returns the string itself. The difference
with repr(object) is that...
|
by: da1978 |
last post by:
Hi experts,
I need to convert a string or a Byte array to a string byte array.
Its relatively easy to convert a string to an char array or a byte array but not a
STRING byte array. i.e.
...
|
by: Debadatta Mishra |
last post by:
Introduction
In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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...
| |