jzuidema:
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?
It's unfortunate that the people who wrote the documentation for the Arduino runtime environment didn't take the trouble to document the API properly so we need to look at the source code to find out what these methods actually do.
When you declare the char array to hold the string content you call the String::length() method. This returns the length excluding the null terminator. (This is the same behaviour as strlen(); it's not unreasonable, but note that your char array needs to be one char longer that this in order to hold the null terminator. Your payload array should be declared as:
uint8_t payload[message.length()+1];
When you copy the characters out of the message, you should supply the actual length of the char array. String::getBytes will populate the first n-1 bytes with the string content and then append the null terminator. If you supply a length which is less than message.length() + 1 then the string would be truncated. If you supply a length which is greater than the actual length of the array then getBytes() may trample over memory outside the bounds of your payload array.
All of these tedious issues would disappear completely if you simply get rid of all uses of String and use c-strings in the first place. Unless you come from a Java background and want an object oriented security blanket to hide you from those scary char arrays, there is no reason for the String class to exist as far as I see. (I am very familiar with Java, and I can see the lengths the Arduino developers have gone to to make C++ behave more like Java, and their reasons for doing so are IMO completely unsound. This sort of stuff is introducing more problems than it solves.)