You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IPAddress.cpp 822B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "Arduino.h"
  2. #include "IPAddress.h"
  3. size_t IPAddress::printTo(Print& p) const
  4. {
  5. int i=0;
  6. while (1) {
  7. p.print(_address.bytes[i], DEC);
  8. if (++i >= 4) return 4;
  9. p.write('.');
  10. }
  11. }
  12. bool IPAddress::fromString(const char *address)
  13. {
  14. unsigned int acc = 0; // Accumulator
  15. unsigned int dots = 0;
  16. while (*address) {
  17. char c = *address++;
  18. if (c >= '0' && c <= '9') {
  19. acc = acc * 10 + (c - '0');
  20. if (acc > 255) {
  21. // Value out of [0..255] range
  22. return false;
  23. }
  24. } else if (c == '.') {
  25. if (dots == 3) {
  26. // Too much dots (there must be 3 dots)
  27. return false;
  28. }
  29. _address.bytes[dots++] = acc;
  30. acc = 0;
  31. } else {
  32. // Invalid char
  33. return false;
  34. }
  35. }
  36. if (dots != 3) {
  37. // Too few dots (there must be 3 dots)
  38. return false;
  39. }
  40. _address.bytes[3] = acc;
  41. return true;
  42. }