473,320 Members | 1,839 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,320 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 2299
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.