How to convert string array to string?

Asked

Viewed 290 times

3

In a program for Arduino, I have an array of strings but it seems that the function where I will use this data only accepts string. How do I convert an array to a string ?

File prog = SD.open("prog.bin");
String leiaS(void){

    return String(prog.read());


 }


   digitalWrite(leia(), inverte(leiaS()));


String inverte(String port){
     if(digitalRead(port)==HIGH)
      return LOW;
     else
      return HIGH;
  }

Error:

could not Convert leias from 'String (*)()' to 'String'

I realized I was missing a parenthesis in the function call leiaS. After fixing and checking appeared the error:

cannot Convert 'String' to 'uint8_t {aka unsigned char}' for argument '2' to 'void digitalWrite(uint8_t, uint8_t)'

  • From what I have researched, there is no ready function. I would go from loop inside the array and concatenates a string itself.

  • Yes, but for that I believe I would need to know the size of the array. I know it has 1 byte, because the read() function reads 1 byte at a time. The problem is how much this will be in strings. According to the documentation the function sizeof() returns the number of bytes. I do not know if it serves. If you could exemplify.

  • Carlos, you’re wearing it String where one expects numerals, it seems to me that you need to rewrite all your code to get the result you want.

2 answers

1

You can make an iteration and go reading each character that the read returns and adds to a char array.

char arrayLeitura[20];
int index=0; 
char charLeitura;

File prog = SD.open("prog.bin");
if (prog) {
    while (prog.available()) {
         charLeitura= prog.read();
         arrayLeitura[index++]=charLeitura;
    }
    prog.close();
}

I believe that later you can give one

return String(arrayLeitura);
  • The problem is that there you are picking up more than one byte. The byte is actually a number. Only it seems that the digitalWrite() function only accepts string. And when I try to pick up a char and convert to string it doesn’t work.

  • Within the while I take the byte and convert to char, then at the end you can make my Return that will return a String of the array of char, you tested?

  • I tried that way String leiaS(void){
 char bt = prog.read();
 char cod[1];
 cod[0] = bt;
 return String(cod);
 
 }., which basically does the same, since I want to read only one byte. It results in the error "could not Convert reads from 'String (*)()' to 'String'"

1


leiaS defined as a function (of type String (*)(void))

String leiaS(void) { /* ... */ }

The reference to leiaS on the line of digitalWrite

digitalWrite(leia(), inverte(leiaS)));

is like a parameter for function inverte.
I suppose the inverted function is defined as receiving a string: you cannot pass a pointer to a function.

  • That must be it. I forgot to put the function inverte. I updated.

  • Now I realized that there were missing a parentheses in the function. After correcting, give the error " cannot Convert 'String' to 'uint8_t {aka unsigned char}' for argument '2' to 'void digitalWrite(uint8_t, uint8_t)'"

  • now you have the second parameter for function digitalWrite with wrong type. Function is set to get a type value uint8_t but you’re passing a type value String. These types are not compatible, so the compiler can not make the conversation from one to the other and error.

  • I traded the returns for unsigned char and resolved

Browser other questions tagged

You are not signed in. Login or sign up in order to post.