1

currently compiling a C programming for a PIC mcu in MPLAB IDE. I just want to count the length of a string and I'm using the strlen function. But it keep return the value 0. Below is my coding:

In Main loop I got this function:

BYTE RXDATA[128];

CountData(RXDATA);

And the CountData function is as below:

BYTE CountData(BYTE* pData)
{
    BYTE nLen;
    nLen = strlen((char*)pData);
    return nLen;
}

The actual length of the RXDATA usually is more than 50 but my nLen always give me 0. Do you guys know what's wrong? Or is there any other way to count the length?

Coolguy
  • 2,225
  • 12
  • 54
  • 81
  • Sorry, the CountData function will return nLen – Coolguy Sep 24 '14 at 02:15
  • 2
    Where does the data in `RXDATA` come from? Could you show how you fill the `RXDATA` with data, and call `CountData`? – Sergey Kalinichenko Sep 24 '14 at 02:17
  • 3
    `strlen()` stops whenever it encounters a `'\0'`. Check `pData[0]`. – timrau Sep 24 '14 at 02:18
  • The code you gave us is neither compilable nor does it display any problem. You need to show us how you fill `RXDATA` and what the value of its elements are. Otherwise we have to assume that `strlen` works and the problem is on your end. – Ed S. Sep 24 '14 at 02:21
  • 1
    What is the type and the size of the type underlying `BYTE` and what is the value of `CHAR_BIT` from ``? It probably isn't the problem, but if `BYTE` was a 16-bit type and `char` an 8-bit type and the data was big-endian, then you could get a string length of zero very easily. – Jonathan Leffler Sep 24 '14 at 02:28
  • 2
    I'm guessing that you mistakenly believe that `strlen` will tell you how long an array is. `strlen` simply counts forward, a byte at a time, until it encounters a null (zero) byte. If your data is not character data it can easily contain null bytes in the middle of the data. – Hot Licks Sep 24 '14 at 02:36

2 Answers2

3

For a start, your call to CountData() needs to store the value somewhere, such as with:

int x = countData(RXDATA);

But think about this for a while. Untold millions of people use strlen every day without it failing. The number of people who have tested your code would probably be in the low single digits. Where do you think the problem would lie?

:-)

If your strlen is returning zero, it's almost certainly because the first byte of your input buffer is a \0, meaning the string has a length of zero.

Temporarily change:

BYTE RXDATA[128];
CountData(RXDATA);

into something like:

BYTE RXDATA[128];
strcpy (RXDATA, "Pax");
int x = CountData(RXDATA); // then check x.

and see what happens. I suspect you'll see three as the return value.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

perhaps you actually want

int x  = sizeof(RXDATA);

Here is a gcc program (that I named `dd.c') that demonstrates this

#include <stdio.h>

unsigned char RXDATA[80];

int main () {
    printf(" sizeof RXDATA = %d\n", (int) sizeof(RXDATA));
}

Compiling it

$ gcc dd.c -o ddd

Running it

$ ./ddd
 sizeof RXDATA = 80
$  

See also related thread sizeof- function or macro?

user50619
  • 325
  • 5
  • 14