0

I'm trying to convert a char array to a char. i used this code

char code[4] = {'1','2','3','4'};

char strcode[4];



void setup()
{
    Serial.begin(9600);
}


void loop()
{
    memcpy(strcode, code, 4);
    Serial.println(strcode);
    Serial.println("done");
    delay(1000);
}

But for some reason when i read my serial monitor i get this:

1234
done

Why are there 2 squares behind it? Am I doing it wrong?

EDIT: For some reason stackexchange doesn't show the 2 squares but there are 2 squares behind 1234

3

C strings need to be NUL-terminated.

char strcode[5];

 ...

 ...
     ...
    strcode[4] = '\0';
     ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.