473,396 Members | 2,037 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,396 software developers and data experts.

pls help: very odd behaviour when concatenating strings

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.ToCharA rray());

I hope I could made myself clear!

Thanks,

Juan.

Nov 16 '05 #1
4 2308
Juan <ju*****************@ANTISPAMhotmail.com> wrote:
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.ToCharA rray());

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".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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" <ju*****************@ANTISPAMhotmail.com> wrote in message news:<O3*************@tk2msftngp13.phx.gbl>...
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.ToCharA rray());

I hope I could made myself clear!

Thanks,

Juan.

Nov 16 '05 #3
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]" <sk***@pobox.com> escribió en el mensaje
news:MP************************@msnews.microsoft.c om...
Juan <ju*****************@ANTISPAMhotmail.com> wrote:
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.ToCharA rray());

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".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
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" <az****@yahoo.com> escribió en el mensaje
news:28**************************@posting.google.c om...
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" <ju*****************@ANTISPAMhotmail.com> wrote in message

news:<O3*************@tk2msftngp13.phx.gbl>...
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.ToCharA rray());

I hope I could made myself clear!

Thanks,

Juan.

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ben | last post by:
Hi all, I'm trying to figure out how how complex map, filter and reduce work based on the following piece of code from http://www-106.ibm.com/developerworks/linux/library/l-prog.html : ...
4
by: sankofa | last post by:
hi, i can't seem to be able to escape my single quote properly... is it even possible in javascript? this is a portion of my code.. var DLEWIS="Pastor Lewis"; .... Sermon is a yser-defined...
7
by: duckfreezone | last post by:
Hi, I've got a small test program which behaves correctly (IMHO) on all compilers that I have acccess to, with the exception of the gcc on Macintosh OSX "gcc version 4.0.0 (Apple Computer, Inc....
4
by: Juan | last post by:
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...
7
by: Mike P | last post by:
I read somewhere that if you are concatenating more than 2 or 3 strings you should use StringBuilder as it will use up less resources. Does this apply when you are building up a string (for...
4
by: FB's .NET Dev PC | last post by:
Interesting note, the code below as is will attempt to cast what is clearly indicated as a string into a double. This is becuase the use of + as a concatenation operator. The error message...
43
by: ZillionDollarSadist | last post by:
Hello, I'm working at a simple Access '97 + VB4 application, and I ran into a terrible problem: something I never modified now gives me a totally unwanted "Invalid use of null" error. It happens...
21
by: c | last post by:
Hi everybody. I'm working on converting a program wriiten on perl to C, and facing a problem with concatenate strings. Now here is a small program that descripe the problem, if you help me to...
4
by: clinisbut | last post by:
I'm not sure if this is the right group, but I didn't found any other more appropiate to post my problem. I'm trying to concatenate chars using the Glib library and I'm getting strange...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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 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.