why does strlen output [00]?

13 Oct 2012

I am using the MODSERIAL library for my serial communication needs especially for RX interrupts. That seems to be working as expected. I noticed when I use the move method provided by the MODSERIAL library, modserial doesn't clear my char array that is storing the RX serial data after I am through using the data. So I did: <<code>>memset(pcResponseValue, '\0', sizeof(pcResponseValue)); <</code>> That seems to work ok; however, I noticed when I set the strlen of the pcREsponseValue char array to a int var then putc to view the results, putc outputs [00] instead of a 0. Why does putc output [00] instead of a 0? When I type text such as hello into my terminal program, putc will output [05]. The 5 seems correct as that number is the amount of characters in the word 'hello'. My code is as follows:

void pcRecvInterrupt(MODSERIAL_IRQ_INFO *q) {
    MODSERIAL *serial = q->serial;
    serial->move(pcResponseValue, 512);
    serial->rxBufferFlush();
    pcResponse = true;
}

    while(1) {
        wait(2.0);
        
        int szArray = strlen(pcResponseValue);
        pc.putc(szArray);
        memset(pcResponseValue, '\0', sizeof(pcResponseValue));
    }
13 Oct 2012

What exactly if your goal in the first place? Is it relevant that it doesn't clear the char array? And your putc seems to be writing binary data, not ascii.

13 Oct 2012

You are trying to print an integer value as a character, and since it's unprintable your terminal program replaces it by the numerical code. To print an integer as a string, use something like this:

pc.printf("array length: %d\n", szArray);
14 Oct 2012

Erik, My goal is to replace the original values with \0 before the MODSERIAL move function copies the RX buffer to my char pcResponseValue char array. For example: lets assume I have testvalue as the char values in pcResponseValue. Next I type test I will get an output of testvalue. Without using memset(&pcResponseValue[0], '\0', sizeof(pcResponseValue)); I am assuming if the new values do not equal or exceed the current char amount the remaining chars will still remain?

14 Oct 2012

That worked. I went back and read more on printf. It makes sense now.

Igor Skochinsky wrote:

pc.printf("array length: %d\n", szArray);