strlen returning zero for string

Thread Starter

ep.hobbyiest

Joined Aug 26, 2014
184
Hi,
i have one array of uint8_t which has 128 element's.
i have copied string which is being received to serial to array. (Data bytes are 50 only).

When i check length for array it is return value as 0. even tough data is present in the string.

how strlen is works?
do i have to append '\0' to string? i tried this one but not working..
 

danadak

Joined Mar 10, 2018
4,057
Generally speaking this calcs the length up to the terminating NULL character.

So is that in location 0 of array, a NULL char ?

Regards, Dana.
 

eetech00

Joined Jun 8, 2013
2,349
Hi,
i have one array of uint8_t which has 128 element's.
i have copied string which is being received to serial to array. (Data bytes are 50 only).

When i check length for array it is return value as 0. even tough data is present in the string.

how strlen is works?
do i have to append '\0' to string? i tried this one but not working..
Which language are you using?
 

nsaspook

Joined Aug 27, 2009
8,377
We need to see code but be sure to start your copy into the string array at position [0] not [1] if you are indexing bytes into the array with a calculated offset.
 

WBahn

Joined Mar 31, 2012
26,398
Hi,
i have one array of uint8_t which has 128 element's.
i have copied string which is being received to serial to array. (Data bytes are 50 only).

When i check length for array it is return value as 0. even tough data is present in the string.

how strlen is works?
do i have to append '\0' to string? i tried this one but not working..
It would appear you are using C (or one of its derivatives).

Is the data that you are putting in this array ASCII codes for characters, or just 8-bit unsigned integers?

The strlen() function is a string function and it assumes that the array is NUL-terminated array of ASCII codes starting at the memory address you pass it. It then walks down that array until it finds a NUL character (which is a zero) and returns the number of preceding non-NUL characters. So if you have integer data and the first data value happens to be zero, it will return zero. Note that this can also happen if you are using larger data elements, such as 16-bit unicode, on a big-endian machine because the upper byte is zero for the ASCII subset.
 

eetech00

Joined Jun 8, 2013
2,349
Hi,
i have one array of uint8_t which has 128 element's.
i have copied string which is being received to serial to array. (Data bytes are 50 only).

When i check length for array it is return value as 0. even tough data is present in the string.

how strlen is works?
do i have to append '\0' to string? i tried this one but not working..
It would appear you are using C (or one of its derivatives).

Is the data that you are putting in this array ASCII codes for characters, or just 8-bit unsigned integers?

The strlen() function is a string function and it assumes that the array is NUL-terminated array of ASCII codes starting at the memory address you pass it. It then walks down that array until it finds a NUL character (which is a zero) and returns the number of preceding non-NUL characters. So if you have integer data and the first data value happens to be zero, it will return zero. Note that this can also happen if you are using larger data elements, such as 16-bit unicode, on a big-endian machine because the upper byte is zero for the ASCII subset.
hmm...I didn't think you could check the length of an array in C with strlen()?

eT
 

WBahn

Joined Mar 31, 2012
26,398
hmm...I didn't think you could check the length of an array in C with strlen()?

eT
Depends on what you mean by checking the length of an array. If you mean finding the size for which an array is allocated for, you can't. The strlen() function does one very simple thing. It interprets the value that is passed to it as a memory address -- what it actually is doesn't matter one bit. It then assumes that an NUL-terminated string of single-byte character codes are stored in memory starting at that location and returns the number of non-NUL codes in memory starting at that location. That's all it does. If that matches what you need to do, then you can use it. If it doesn't, then don't.
 

eetech00

Joined Jun 8, 2013
2,349
Depends on what you mean by checking the length of an array. If you mean finding the size for which an array is allocated for, you can't. The strlen() function does one very simple thing. It interprets the value that is passed to it as a memory address -- what it actually is doesn't matter one bit. It then assumes that an NUL-terminated string of single-byte character codes are stored in memory starting at that location and returns the number of non-NUL codes in memory starting at that location. That's all it does. If that matches what you need to do, then you can use it. If it doesn't, then don't.
Yes...I was thinking size_of array...nevermind…

eT
 
Top