IPaddress class to const char*

Got a project going on an ESP32.

I'm using several libraries. They all need to know the IP address of a remote server. NOT the machine's local IP address, the IP address of another machine.

Most of them want IP addresses in the form of a byte array, and they seem to be happy with Arduino's weird, non-standard, practically undocumented IPAddress class.

But there's always that one guy.

EspMQTTClient requires that the MQTT Broker's IP address be input as a const char * of the format "192.168.1.1"
. And for the life of me I can't figure or find out how to convert an IPAddress object or a byte array into a const char *.

I can convert it into a String object via the following shenanigan:

String serverAddressString = String(
  String(serverAddress[0]) + '.' +
  String(serverAddress[1]) + '.' +
  String(serverAddress[2]) + '.' +
  String(serverAddress[3]));

But even from here I can't get the damn thing converted to a form that his Royal High And Mightiness EspMQTTClient will accept. From the EspMQTTClient Github, for reference:

  EspMQTTClient(
    const char* wifiSsid,
    const char* wifiPassword,
    const char* mqttServerIp,
    const char* mqttUsername,  // Omit this parameter to disable MQTT authentification
    const char* mqttPassword,  // Omit this parameter to disable MQTT authentification
    const char* mqttClientName = "ESP8266",
    const short mqttServerPort = 1883);

How do I get this done?

Have you tried sprintf()?

Once you have it in a String, you can use the String method c_str() to get a C string. But if you're going to that trouble, why not use similar shenanigans to build a C string in a char array directly?

wildbill:
Once you have it in a String, you can use the String method c_str() to get a C string. But if you're going to that trouble, why not use similar shenanigans to build a C string in a char array directly?

c_str() got it.

I wasn't aware you could concatenate char arrays. Doesn't GCC have a fit if you don't declare the length of an array at compile time?

Look at strcat for example, or sprintf as suggested. But since you have the String now, maybe just use c_str and move on. It's a bit of a waste of memory is all.

That's okay, I've got 520k of ram. Might as well use it for something.

wildbill:
It's a bit of a waste of memory is all.

Also, kind of hacky. Converting an integer to a String object just so you can then convert it to an array of ASCII char. There are much simpler ways of going from an integer to an ASCII string.

String serverAddressString = String(

String(serverAddress[0]) + '.' +
 String(serverAddress[1]) + '.' +
 String(serverAddress[2]) + '.' +
 String(serverAddress[3]));

... or just

String serverAddressString = serverAddress.toString();