The methods for getting the length of a string in C++/Arduino Wire all seem to count up to a null terminating byte, then they either include it in the length or not and then return the length up to then. Well lets say I have a String like this: ‘h’,‘e’,‘l’,‘l’,‘o’,’\0’,‘t’,‘h’,‘e’,‘r’,‘e’ Now if you call length on this string you will get 5 as the length. I need a call on this string that will return 11. Is this possible?
Well, this is not a string, according to C.
I’m afraid you should see it (and treat it) as a chars sequence rather than a string.
Thank you @duffo64
I have a method which returns a String object. The string object can contain null bytes. I need to create a byte array containing all bytes of the String including the null bytes. Any ideas?
Would you mind showing some code @IOTrav ? Just curious to see in detail what we are talking about …
Really agree with @duffo64 here–we need to see some example code.
Arduino String objects use strlen() etc. internally for lots of operations and that won’t work past the \0 char anyway. They are just not set up for arrays that include \0 bytes and you probably just need to use a different data structure.
How are you creating this String and how are you trying to use it? How do you know how many characters are in the String?
While providing your code would help to understand your problem, I - as most, if not all, C programmers - would advise you NOT to use a zero-terminator inside a string.
Whatever standard function you’ll want to pass that string to, will not care if your something-like-a-string String goes beyond that zero-terminator - as the term suggests the zero-terminator terminates the string, so everything beyond it actually isn’t a part of the string anymore.
Just as if you had a string of wool and then you cut (terminate) it at some position. The thing beyond that cut is not part of your string anymore. It’s some other string now.
What is happening is I am communicating back and forth with a PHP based server. All communications are AES CBC 128 encrypted which can contain null or \0 bytes. Communication is via HTTP Requests. I am using nmattisson’s http library right now on the spark for the HTTP Requests/Responses. As many of you probably know an http request is really just a TCP socket which communicates bytes back and forth and then a header is included. Here is a link to his library:
I think the best solution is to modify this library to return an unsigned char array rather than a String for the Response.body So that is what I am working on now. I was just hoping this modification would not be required.
A standard C++ string however has a method called .data() which seems to ignore null terminating bytes. I see however the Arduino Wire String does not implement this method. Not sure if it would have worked for my purpose or not though.
Thank you everyone for the help.