convert string to char array

Hi guys, I have a problem. I have to convert a String to a char array to codify it in Base64. This is the code:

 if (portaS->available() > 0) 
        {
          serialRead = portaS->read();
          char input[] = serialRead;
          int inputLen = sizeof(input);
          int encodedLen = base64_enc_len(inputLen);
          char encoded[encodedLen];
          base64_encode(encoded, input, inputLen); 
          client.println(encoded);
          client.flush();
          client.stop();
        }

Doesn't the String class have a method to do this?

AWOL: Doesn't the String class have a method to do this?

Yes the method is the toCharArray(), but I don't know how to use it. I've looked for some examples, but i don't understand how to use it properly.

but I don't know how to use it. I've looked for some examples, but i don't understand how to use it properly.

String aStupidWasteOfResource = "Pissing away resources uselessly";
char copy[50];
aStupidWasteOfResource.toCharArray(copy, 50);
1 Like

Thanks for help at all but i've solved it this way:

if (portaS->available() > 0) 
        {
          serialRead = portaS->read();
          char charBuf[serialRead.length() + 1];
          serialRead.toCharArray(charBuf, serialRead.length());
          int inputLen = sizeof(serialRead);
          int encodedLen = base64_enc_len(inputLen);
          char encoded[encodedLen];
          base64_encode(encoded, input, inputLen); 
          client.println(encoded);
          client.flush();
          client.stop();
        }

Thanks to all agin.

          serialRead = portaS->read();

So, the String is one character/byte long? Really? And you need to waste (mod edit) away resources using a String to wrap, and then immediately unwrap, one character? Get real.

The String class is really a crutch in most cases…easy to use, but a horrible waste of memory. Your version of the code below uses 3622 bytes of memory.

void setup() {

  Serial.begin(9600);

  char copy[50];
 // strcpy(copy, "Pissing away resources uselessly");    // Uncomment for string use

  String aStupidWasteOfResource = "Pissing away resources uselessly";  // Comment these two out...
  aStupidWasteOfResource.toCharArray(copy, 50);

  Serial.println(copy);
}

void loop() {
}

Comment out the String class variable and method (note uppercase ‘S’) and uncomment the call to strcpy() and the code size shrinks to 2166. There are few things the String class brings to the table that you can’t do with the string processing functions offered in the C string library. See:

http://www.tutorialspoint.com/c_standard_library/string_h.htm

1 Like

There are few things the String class brings to the table that you can't do with the string processing functions offered in the C string library.

You misspelled "There is nothing..." 8)

Hola de nuevo Surbyte, gracias por tu respuesta…si funciona, pero no enciende el rele, como que no hace la comparacion del if…

#include <SoftwareSerial.h>

String cadena;

void setup()
{
  Serial.begin(9600);
  //Serial.setTimeout(500);
  pinMode(5, OUTPUT);
  digitalWrite(5, HIGH);
}
void loop()
{
  if (Serial.available() > 0) {
  //Serial.println(Serial.readString());
  cadena = Serial.readString();
  //Serial.println("Cadena es igual a:");
  Serial.println(cadena);
  
  if (cadena == 'A') {      //  "A" con comillas tampoco 
      digitalWrite(5, LOW);
  }
  if (cadena == 'B') {
      digitalWrite(5, HIGH);
  }
 }
}

la variable si tiene ahora lo que mando por monitor, pero no se porque en el if no compara o si compara no lo hace correctamente, y al final el rele no se enciende…