-->
Page 1 of 3

Converting IPAddress to String

PostPosted: Thu Jul 09, 2015 6:59 am
by Nico van der Dussen
Hi,

I want to convert an IPAddress to a string in the Arduino IDE

I've tried the following

Code: Select all 
  IPAddress ipno;
char ipno2[26] ;

  ipno = WiFi.localIP();
  sprintf(ipno2, "%d.%d.%d.%d", ipno[0], ipno[1], ipno[2], ipno[3]);


This gives the error:

Code: Select allArduino: 1.6.5 (Windows 8.1), Board: "Generic ESP8266 Module, 80 MHz, 115200, 512K (64K SPIFFS)"

<cut a lot of noise>

AdvancedWebServerMMA.ino:78:2: error: cannot pass objects of non-trivially-copyable type 'class IPAddress' through '...'
cannot pass objects of non-trivially-copyable type 'class IPAddress' through '...'


What is the right way of doing this?

Thanks

Re: Converting IPAddress to String

PostPosted: Thu Jul 09, 2015 7:22 am
by Nico van der Dussen
As it often happens:

Just after posting I discovered the finger trouble that caused all the problems :)

Thanks to all that had a look at this.

Nico

Re: Converting IPAddress to String

PostPosted: Thu Jul 09, 2015 9:25 am
by martinayotte
Personally, I've added a new method in IPAddress class.
In IPAddress.h
Code: Select allchar * toCharArray();

In IPAddress.cpp
Code: Select allchar *IPAddress::toCharArray()
{
    static char szRet[20];
    String str = String(_address.bytes[0]);
    str += ".";
    str += String(_address.bytes[1]);
    str += ".";
    str += String(_address.bytes[2]);
    str += ".";
    str += String(_address.bytes[3]);
    str.toCharArray(szRet, 20);
    return szRet;
}

So, you can easily print IP under almost and Printable, such as Serial.println(Wifi.localIP().toCharArray());

Re: Converting IPAddress to String

PostPosted: Sun Aug 09, 2015 12:22 pm
by kolban
Martin,
That is a nice function. Did you add that to the project's source on Github?

Neil