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

Commit2543716

Browse files
authored
Merge pull request#187 from pennam/ip-toString
IPAddress: add toString() method
2 parentsc6371bc +1a4a7f8 commit2543716

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

‎api/IPAddress.cpp‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,33 @@ IPAddress::IPAddress(const char *address)
9494
fromString(address);
9595
}
9696

97+
StringIPAddress::toString4()const
98+
{
99+
char szRet[16];
100+
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]);
101+
returnString(szRet);
102+
}
103+
104+
StringIPAddress::toString6()const
105+
{
106+
char szRet[40];
107+
sprintf(szRet,"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
108+
_address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3],
109+
_address.bytes[4], _address.bytes[5], _address.bytes[6], _address.bytes[7],
110+
_address.bytes[8], _address.bytes[9], _address.bytes[10], _address.bytes[11],
111+
_address.bytes[12], _address.bytes[13], _address.bytes[14], _address.bytes[15]);
112+
returnString(szRet);
113+
}
114+
115+
StringIPAddress::toString()const
116+
{
117+
if (_type == IPv4) {
118+
returntoString4();
119+
}else {
120+
returntoString6();
121+
}
122+
}
123+
97124
boolIPAddress::fromString(constchar *address) {
98125
if (!fromString4(address)) {
99126
returnfromString6(address);

‎api/IPAddress.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class IPAddress : public Printable {
9696
IPAddress&operator=(constchar *address);
9797

9898
virtualsize_tprintTo(Print& p)const;
99+
StringtoString()const;
99100

100101
IPTypetype() {return _type; }
101102

@@ -110,6 +111,8 @@ class IPAddress : public Printable {
110111
protected:
111112
boolfromString4(constchar *address);
112113
boolfromString6(constchar *address);
114+
StringtoString4()const;
115+
StringtoString6()const;
113116
};
114117

115118
externconst IPAddress IN6ADDR_ANY;

‎test/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ set(TEST_SRCS
2929
src/Common/test_map.cpp
3030
src/Common/test_max.cpp
3131
src/Common/test_min.cpp
32+
src/IPAddress/test_toString.cpp
3233
src/IPAddress/test_fromString.cpp
3334
src/IPAddress/test_fromString6.cpp
3435
src/IPAddress/test_IPAddress.cpp
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2020 Arduino. All rights reserved.
3+
*/
4+
5+
/**************************************************************************************
6+
* INCLUDE
7+
**************************************************************************************/
8+
9+
#include<catch.hpp>
10+
11+
#include<String.h>
12+
#include<IPAddress.h>
13+
14+
/**************************************************************************************
15+
* TEST CODE
16+
**************************************************************************************/
17+
18+
TEST_CASE ("Extract valid string from IPv4address","[IPAddress-toString-01]")
19+
{
20+
arduino::IPAddressip(129,168,1,2);
21+
22+
REQUIRE(ip.toString().equals("129.168.1.2") ==true);
23+
}
24+
25+
TEST_CASE ("Extract valid ipv6 string from IPv6address","[IPAddress-toString-02]")
26+
{
27+
arduino::IPAddressip(0x20,0x01,0xd,0xb8,1,2,3,4,5,6,7,8,9,0xa,0xb,0xc);
28+
29+
REQUIRE(ip.toString().equals("2001:0db8:0102:0304:0506:0708:090a:0b0c") ==true);
30+
}
31+
32+
TEST_CASE ("Extract 0.0.0.0 string from uninitialized IP address","[IPAddress-toString-03]")
33+
{
34+
arduino::IPAddress ip;
35+
36+
REQUIRE(ip.toString().equals("0.0.0.0") ==true);
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp