pls help: very odd behaviour when concatenating strings

  • Thread starter Thread starter Juan
  • Start date Start date
J

Juan

Does any one know if there are reported bugs when concatenating strings?

When debugging each variable has the correct value but when I try to
concatenate them some values are missing (I can´t see them in the debugger).
After encoding the string (the sameone which complete value is not visible
from the debugger) all the values can be seen but they are spaced by big
amounts of zeros and use more that the 2048 bytes allocated. It is like if
some of the string variables that I concatenate have a some "unused" space
that can not be seen....

Byte[] buffer = new Byte[2048]
buffer = System.Text.Encoding.ASCII.GetBytes(string.ToCharArray());

I hope I could made myself clear!

Thanks,

Juan.
 
Juan said:
Does any one know if there are reported bugs when concatenating strings?

There are lots of *reported* bugs. Almost all turn out to be user error
or a problem in the debugger.
When debugging each variable has the correct value but when I try to
concatenate them some values are missing (I can´t see them in the debugger).

I suggest not using the debugger to diagnose this, as it can be
confusing when the string contains null characters (character 0).
After encoding the string (the sameone which complete value is not visible
from the debugger) all the values can be seen but they are spaced by big
amounts of zeros and use more that the 2048 bytes allocated. It is like if
some of the string variables that I concatenate have a some "unused" space
that can not be seen....

You haven't actually shown any string concatenation code. However...
Byte[] buffer = new Byte[2048]
buffer = System.Text.Encoding.ASCII.GetBytes(string.ToCharArray());

I hope I could made myself clear!

That doesn't end up with a 2K buffer. It ends up with a buffer of
however long the string is. (You don't need to call ToCharArray, by the
way - just use the version of Encoding.GetBytes which takes a string.)

The first line of your code is effectively useless - you're just
creating an array and then ignoring it in the next line of code, as
you're assigning a new value to "buffer".
 
Juan,

More then likely you are having a problem because your byte array is
not full. As such every element that hasn't been assigned a byte will
be treated as 0 which messes things up when you convert the whole
array to a string.

You need to make sure whenever you do an operation on the array you
use the length of the array to limit the elements you are converting.
Example:

byte[] buffer = new byte[2048];
// Put some stuff in the buffer.
int bufferIndex = length of data placed in the buffer
string bufferString = System.Text.Encoding.ASCII.GetString(buffer, 0,
bufferIndex);

From you code I am not sure what you are trying to do? You already
have a string but you are putting it into a byte array? If possible
you should use a StringBuilder object for string concetentaion
operations, it saves the inefficiency of creating new string objects
for each append.

Regards,

Aaron
 
Thanks a lot, I was able to find the problem. When concatenating the string
I was using a value obtained from a buffer, I was not correctly splitting
the buffer so the resulting variable was es big as the buffer.



"Jon Skeet [C# MVP]" <[email protected]> escribió en el mensaje
Juan said:
Does any one know if there are reported bugs when concatenating strings?

There are lots of *reported* bugs. Almost all turn out to be user error
or a problem in the debugger.
When debugging each variable has the correct value but when I try to
concatenate them some values are missing (I can´t see them in the
debugger).

I suggest not using the debugger to diagnose this, as it can be
confusing when the string contains null characters (character 0).
After encoding the string (the sameone which complete value is not visible
from the debugger) all the values can be seen but they are spaced by big
amounts of zeros and use more that the 2048 bytes allocated. It is like if
some of the string variables that I concatenate have a some "unused" space
that can not be seen....

You haven't actually shown any string concatenation code. However...
Byte[] buffer = new Byte[2048]
buffer = System.Text.Encoding.ASCII.GetBytes(string.ToCharArray());

I hope I could made myself clear!

That doesn't end up with a 2K buffer. It ends up with a buffer of
however long the string is. (You don't need to call ToCharArray, by the
way - just use the version of Encoding.GetBytes which takes a string.)

The first line of your code is effectively useless - you're just
creating an array and then ignoring it in the next line of code, as
you're assigning a new value to "buffer".
 
Thanks a lot, I was able to find the problem. When concatenating the string
I was using a value obtained from a buffer, I was not correctly splitting
the buffer so the resulting variable was es big as the buffer.

Aaron said:
Juan,

More then likely you are having a problem because your byte array is
not full. As such every element that hasn't been assigned a byte will
be treated as 0 which messes things up when you convert the whole
array to a string.

You need to make sure whenever you do an operation on the array you
use the length of the array to limit the elements you are converting.
Example:

byte[] buffer = new byte[2048];
// Put some stuff in the buffer.
int bufferIndex = length of data placed in the buffer
string bufferString = System.Text.Encoding.ASCII.GetString(buffer, 0,
bufferIndex);

From you code I am not sure what you are trying to do? You already
have a string but you are putting it into a byte array? If possible
you should use a StringBuilder object for string concetentaion
operations, it saves the inefficiency of creating new string objects
for each append.

Regards,

Aaron

"Juan" <[email protected]> wrote in message
Does any one know if there are reported bugs when concatenating strings?

When debugging each variable has the correct value but when I try to
concatenate them some values are missing (I can´t see them in the debugger).
After encoding the string (the sameone which complete value is not visible
from the debugger) all the values can be seen but they are spaced by big
amounts of zeros and use more that the 2048 bytes allocated. It is like if
some of the string variables that I concatenate have a some "unused" space
that can not be seen....

Byte[] buffer = new Byte[2048]
buffer = System.Text.Encoding.ASCII.GetBytes(string.ToCharArray());

I hope I could made myself clear!

Thanks,

Juan.
 
Back
Top