problem on method ReadChars

C

chrisben

Hi,

I have a binary reader which read a stream from the network through
MemoryStream, the data in the stream is a 8 bytes char array. it could be a
string with 2 chars to 7 chars. it is garanteed that there will be a
terminator at the end of the string, but there could be garbage to fill the
rest of the array.

For example, the array could be {'a','b','\0', ....}

I use CharToString (reader.ReadChars(8)) to get the data. However, it seems
work only if the rest of array is all filled with terminators. Otherwise,
will get overflow error. As I already have one terminator after the string, I
do not want to fill the rest of the array by initialization. Seems ReadChars
not working in this case, is there other method in C# that I should use to
read string correctly?

Thanks

Chris
 
P

Peter Duniho

Hi,

I have a binary reader which read a stream from the network through
MemoryStream, the data in the stream is a 8 bytes char array. it could
be a
string with 2 chars to 7 chars. it is garanteed that there will be a
terminator at the end of the string, but there could be garbage to fill
the
rest of the array.

For example, the array could be {'a','b','\0', ....}

I use CharToString (reader.ReadChars(8)) to get the data. However, it
seems
work only if the rest of array is all filled with terminators. Otherwise,
will get overflow error. As I already have one terminator after the
string, I
do not want to fill the rest of the array by initialization.

Why not? That seems like an arbitrarily stubborn position. .NET arrays
are already zero-filled on creation anyway. Why would it be so bad for
your own code (whatever it is) to do the same?

Barring that, you may be able to set the encoding for the BinaryReader to
(for example) ISO-8859-1, allowing individual bytes to be treated as
valid, or set the Encoding in use to ASCII and set the DecoderFallback
property to DecoderReplacementFallback (or maybe it already is by
default...I don't recall). Either way, when you actually use the string,
you'll still have to concern yourself with the null terminator, unless the
string is consumed only in a context where the null terminator is handled
as you expect.

Finally, you may want to consider simply reading an eight-byte array,
searching in the array for the null-terminator, and then using one of the
decoding methods that allows you to specify the number of bytes from an
array to actually decode.

Pete
 
C

chrisben

Thanks for the advice, Pete. Actually, my .NET read socket data sent from a
linux box. Since I deal with tons of data, I would like to use the MOST the
efficient way.

So let me redefine the problem

for a fixed size 8 byte array, string size may vary between 2-7, there is
always a terminator at the end of the string, however, the value of the rest
of array is undetermined (due to the bad coding at linux side).

So my question now is:

what is the best way to read the valid string from this byte array using
..net method, with the least overhead? I used CharToString(readChars(8)), it
did not work unless all the rest of array filled with '/0'. (i can seach the
array for null, but i feel there should be a better way to do it in C#)

Thanks
 
P

Peter Duniho

Thanks for the advice, Pete. Actually, my .NET read socket data sent
from a
linux box.

From code you control, or not?
Since I deal with tons of data, I would like to use the MOST the
efficient way.

You're putting the cart before the horse. Get it working first. Then,
see if the solution performs well enough (hint: I guarantee it
will...network i/o is far too slow for these kinds of microoptimizations
on the processing of the data to matter).
So let me redefine the problem

for a fixed size 8 byte array, string size may vary between 2-7, there is
always a terminator at the end of the string, however, the value of the
rest
of array is undetermined (due to the bad coding at linux side).

So my question now is:

what is the best way to read the valid string from this byte array using
.net method,

I described a number of possibilities, all of which should be fine.
with the least overhead?

None of the solutions I mentioned, if implemented correctly, should
interfere with the throughput of your network i/o.
I used CharToString(readChars(8)), it
did not work unless all the rest of array filled with '/0'. (i can seach
the
array for null, but i feel there should be a better way to do it in C#)

Some code somewhere has to search for and detect the null. It isn't going
to matter whether that code is in your own code, or you are calling some
hypothetical library method that does that for you.

Assuming you can't modify the way the data is sent, IMHO the best solution
is simply to read the data as an eight-byte array, search for the
terminator yourself, and use that information to decode the bytes to text
(e.g. System.String). But even if you created whole new Encoding and
DecoderReplacementFallback instances each time you read these eight bytes,
I doubt you'd notice the overhead in the network i/o throughput.

Pete
 

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