Arduino: How to get IP address from String

Created on 24 Dec 2016  路  3Comments  路  Source: esp8266/Arduino

I have esp8266 with webserver. Now I want changr the IP address from web.

How to convert: String strIP = "192.168.4.1" to IPAddress ip(192, 168, 4, 1)

Thank.

Most helpful comment

IPAddress addr;
if (addr.fromString(strIP)) {
  // it was a valid address, do something with it 
}

All 3 comments

Something along these lines should work.

int Parts[4] = {0,0,0,0};
int Part = 0;
for ( int i=0; i<strIP.length(); i++ )
{
    char c = strIP[i];
    if ( c == '.' )
    {
        Part++;
        continue;
    }
    Parts[Part] *= 10;
    Parts[Part] += c - '0';
}
IPAddress ip( Parts[0], Parts[1], Parts[2], Parts[3] );

Stackoverflow would be a better place to ask this sort of question, it's not an issue :)

IPAddress addr;
if (addr.fromString(strIP)) {
  // it was a valid address, do something with it 
}

@hoacvxd not the right place to ask these questions.
Closing due to off-topic, see #3655 .

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pablotix20 picture pablotix20  路  3Comments

dariopb picture dariopb  路  3Comments

hulkco picture hulkco  路  3Comments

markusschweitzer picture markusschweitzer  路  3Comments

Geend picture Geend  路  3Comments