Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

IPAddress: add toString() method#187

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
facchinm merged 2 commits intoarduino:masterfrompennam:ip-toString
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletionsapi/IPAddress.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,6 +94,33 @@ IPAddress::IPAddress(const char *address)
fromString(address);
}

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

String IPAddress::toString6() const
{
char szRet[40];
sprintf(szRet,"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
_address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3],
_address.bytes[4], _address.bytes[5], _address.bytes[6], _address.bytes[7],
_address.bytes[8], _address.bytes[9], _address.bytes[10], _address.bytes[11],
_address.bytes[12], _address.bytes[13], _address.bytes[14], _address.bytes[15]);
return String(szRet);
}

String IPAddress::toString() const
{
if (_type == IPv4) {
return toString4();
} else {
return toString6();
}
}

bool IPAddress::fromString(const char *address) {
if (!fromString4(address)) {
return fromString6(address);
Expand Down
3 changes: 3 additions & 0 deletionsapi/IPAddress.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -96,6 +96,7 @@ class IPAddress : public Printable {
IPAddress& operator=(const char *address);

virtual size_t printTo(Print& p) const;
String toString() const;

IPType type() { return _type; }

Expand All@@ -110,6 +111,8 @@ class IPAddress : public Printable {
protected:
bool fromString4(const char *address);
bool fromString6(const char *address);
String toString4() const;
String toString6() const;
};

extern const IPAddress IN6ADDR_ANY;
Expand Down
1 change: 1 addition & 0 deletionstest/CMakeLists.txt
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,7 @@ set(TEST_SRCS
src/Common/test_map.cpp
src/Common/test_max.cpp
src/Common/test_min.cpp
src/IPAddress/test_toString.cpp
src/IPAddress/test_fromString.cpp
src/IPAddress/test_fromString6.cpp
src/IPAddress/test_IPAddress.cpp
Expand Down
37 changes: 37 additions & 0 deletionstest/src/IPAddress/test_toString.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2020 Arduino. All rights reserved.
*/

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include <catch.hpp>

#include <String.h>
#include <IPAddress.h>

/**************************************************************************************
* TEST CODE
**************************************************************************************/

TEST_CASE ("Extract valid string from IPv4address", "[IPAddress-toString-01]")
{
arduino::IPAddress ip(129,168,1,2);

REQUIRE(ip.toString().equals("129.168.1.2") == true);
}

TEST_CASE ("Extract valid ipv6 string from IPv6address", "[IPAddress-toString-02]")
{
arduino::IPAddress ip(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc);

REQUIRE(ip.toString().equals("2001:0db8:0102:0304:0506:0708:090a:0b0c") == true);
}

TEST_CASE ("Extract 0.0.0.0 string from uninitialized IP address", "[IPAddress-toString-03]")
{
arduino::IPAddress ip;

REQUIRE(ip.toString().equals("0.0.0.0") == true);
}

[8]ページ先頭

©2009-2025 Movatter.jp