Skip to content
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

IPAddress::toString() needed #38

Closed
hsaturn opened this issue Sep 18, 2021 · 2 comments
Closed

IPAddress::toString() needed #38

hsaturn opened this issue Sep 18, 2021 · 2 comments

Comments

@hsaturn
Copy link

hsaturn commented Sep 18, 2021

Hello Brian, could you please add this patch to IPAddress

I need this in order to enhance my tests around EspMock / TinyMqtt

Best regards

diff --git a/cores/epoxy/IPAddress.cpp b/cores/epoxy/IPAddress.cpp
index a7df382..3e835d6 100644
--- a/cores/epoxy/IPAddress.cpp
+++ b/cores/epoxy/IPAddress.cpp
@@ -114,3 +114,17 @@ size_t IPAddress::printTo(Print& p) const
     n += p.print(_address.bytes[3], DEC);
     return n;
 }
+
+String IPAddress::toString() const
+{
+    String s;
+
+    size_t n = 0;
+    for (int i =0; i < 3; i++)
+    {
+        s += String(_address.bytes[i]);
+        s += '.';
+    }
+    s += String(_address.bytes[3]);
+    return s;
+}
diff --git a/cores/epoxy/IPAddress.h b/cores/epoxy/IPAddress.h
index 2c0c1b6..5e1e504 100644
--- a/cores/epoxy/IPAddress.h
+++ b/cores/epoxy/IPAddress.h
@@ -67,6 +67,7 @@ public:
     IPAddress& operator=(uint32_t address);
 
     virtual size_t printTo(Print& p) const;
+    String toString() const;
 };

 const IPAddress INADDR_NONE(0,0,0,0);
@bxparks
Copy link
Owner

bxparks commented Sep 18, 2021

As far as I can tell, IPAddress::toString() is defined only for ESP8266 and ESP32. So I guarded the method around a #if defined(EPOXY_CORE_ESP8266) so that it is available when EPOXY_CORE := EPOXY_CORE_ESP8266 is defined in the Makefile.

Added some tests to validate.

If you need this on AVR, STM32, and other non-ESP platforms, you can use the printTo(Print&) function into the PrintStr<N> class from my AceCommon library. See the IPAddressTest.ino file for an example).

@bxparks bxparks closed this as completed Sep 18, 2021
@hsaturn
Copy link
Author

hsaturn commented Sep 19, 2021

Thanks a lot Brian

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

No branches or pull requests

2 participants