Error in String.getBytes() ?

PeterH:

jzuidema:
Hmmm ok, dont wanna argue, but the way I did it consistently yields the correct result (varying String size/chars)

Except it doesn't, does it? You said that one of your attempts truncated the string, and the other one appended a spurious space. Both of these are direct consequences of the bugs in the way you declare the array and copy the received bytes in it.

I suggest you drop all use of the String class and use plain old c-strings (null-terminated char arrays). It's no harder than using Strings, avoids the possibility of memory corruption introduced by the String class, and avoids the messy copying between Strings and c-strings which is causing you the problem here.

You didnt understand me than. I told of various combinations of using length() and length()+1 in declaring the array and filling it, with varying results. I also told that my combination (array length(), getBytes() length+1) worked well consistently, and still does.

What didnt work was the advice given by PaulS, which resulted in empty trailing bytes with gibberish (array length()+1, getBytes() length()). So I still dont understand why I should not be using it this/my way, when the adviced ways, which apparently should result in the proper behavior, dont work. Note Im using 1.04, so the String errors should be fixed right?

As for using char arrays, Ill look into that in a later stage. Aside from memory use, I dont really see, even with given advice in this thread and elsewhere, why I should switch perse?! Anyways Ill look into ways of replacing the methods I use for String (getBytes()/concat etc) for methods using char arrays if you guys insist... :slight_smile: