473,379 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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 2807

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
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...
6
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...
15
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?
6
by: Allan Ebdrup | last post by:
How do I easily convert a int to a string? Kind Regards, Allan Ebdrup
6
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...
6
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...
12
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} ...
19
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...
5
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. ...
0
Debadatta Mishra
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.