Skip to content

add toCharArray() to IPAddress class #714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 31, 2015
Merged

add toCharArray() to IPAddress class #714

merged 3 commits into from
Aug 31, 2015

Conversation

martinayotte
Copy link
Contributor

I was tired to grab/merge this personal change from one version to the other.

@igrr
Copy link
Member

igrr commented Aug 21, 2015

Why not add a "toString" method which would return a String? This way you could avoid the hack with a static char array.

@sdrsdr
Copy link

sdrsdr commented Aug 21, 2015

why not snprintf to the buffer:

snpintf(szRet,20,"%u.%u.%u.%u",(unsigned int) _address.bytes[0],(unsigned int) _address.bytes[1],(unsigned int) _address.bytes[2],(unsigned int) _address.bytes[3]);

@Links2004
Copy link
Collaborator

using static char szRet[20]; will restiveness 20 byte even when you never use this function.
i think the string way is the better one. you can easy make a char array out of the String.

i prefer

String IPAddress::toString()
{
     char szRet[16];
     sprintf(szRet,"%u.%u.%u.%u", _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3]);
    return String(szRet);
}

or for char*

char * IPAddress::toChar(char * buf)
{
    sprintf(buf, "%u.%u.%u.%u", _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3]);
    return buf;
}

then its the same way we do it for the mac for example:
https://github.com/esp8266/Arduino/blob/esp8266/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp#L296

@martinayotte
Copy link
Contributor Author

Ok ! I've pushed the first suggestion from Links2004.
I didn't took second suggestion, since it requires the caller to provide the storage, and that why I used static in the first version. Callers can still do ipaddr.toString().c_str() if "char *" is required.

@martinayotte
Copy link
Contributor Author

The following thread is meaningful for my last commit :
#596

igrr added a commit that referenced this pull request Aug 31, 2015
add toCharArray() to IPAddress class
@igrr igrr merged commit 944b22e into esp8266:esp8266 Aug 31, 2015
igrr added a commit that referenced this pull request Oct 29, 2015
add toCharArray() to IPAddress class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants