Is there a strlen method that goes until last null?

Hi there, title is my question.

I have a unsigned char recieved_data[10000]; that gets filled with some characters, among them are also multiple null characters.

Now I need to figure out how many characters were written in that array but somehow I couldn't find yet a (good) solution for that problem.
The best thing would be a method that reads until the last null character because the last char is always null.

Any help is much appreciated.
Last edited on
If you initialize your array to NULL and set old values to NULL, then you could check if there are two NULL values in a row. That would signal your last NULL.
How do you know when the sequence ends?
@Stewbond: sadly it wouldn't work that way because two or more null characters are also written into the array

@kbw: umm, idk I guess the last null char that I mentioned would be useless if I write some smaller data again into the array and the old chars would still be in the array?
I'm using boost asio async_read_some where I pass this array and when I recieve something (over the network) it gets written into the array until its finished (though I'm not exactly sure when he knows that).
Perhaps you could start at the last element of the array and work backwards. The first non-NULL value you find will be the element before the last NULL.
The read function should tell you how many characters were read. You should keep this length data with the buffer as there's no way to mark the end of sequence in the buffer itself. Plus it's faster to keep the length with the buffer.
and what if it would be like I described it before?

for example I get a char sequence with a size of 75 that gets written into the array. from that I could determine the size.
but then I get a char sequence with a size of 30, in that case the old chars are still in the array right? so I would still get a size of 75 from that array or?

besides that, is there really no other way than going through the array with a loop? because my array has a size of ~10000 (dont know why I wrote 100, I'll change that) and I think that would require a little more noticeable time.

edit: @kbw: I guess you are right, I'll try to get it working because in all the code sample I saw of async_read I never saw something like "bytes_transfered".
Last edited on
for example I get a char sequence with a size of 75 that gets written into the array. from that I could determine the size.
but then I get a char sequence with a size of 30, in that case the old chars are still in the array right? so I would still get a size of 75 from that array or?
How would you get the size of the array? The point is you can't. Only the read method knows how much stuff what placed in the array. So you have to preserve that information along with the array.
thanks I have it now working, should have thought of that before :P
Topic archived. No new replies allowed.