fixed MacAddr 0 output

This commit is contained in:
Erik Frojdh 2019-05-17 09:08:13 +02:00
parent fff79fbbb6
commit 591ff53b84
2 changed files with 8 additions and 14 deletions

View File

@ -20,9 +20,7 @@ IpAddr::IpAddr(const std::string &address) {
inet_pton(AF_INET, address.c_str(), &addr_);
}
IpAddr::IpAddr(const char *address) {
inet_pton(AF_INET, address, &addr_);
}
IpAddr::IpAddr(const char *address) { inet_pton(AF_INET, address, &addr_); }
std::string IpAddr::str() const {
char ipstring[INET_ADDRSTRLEN]{};
@ -31,9 +29,9 @@ std::string IpAddr::str() const {
}
std::string IpAddr::hex() const {
std::ostringstream ss;
ss << std::hex << std::setfill('0');
for (int i = 0; i != 4; ++i) {
ss << std::hex << std::setfill('0') << std::setw(2)
<< ((addr_ >> i * 8) & 0xFF);
ss << std::setw(2) << ((addr_ >> i * 8) & 0xFF);
}
return ss.str();
}
@ -56,18 +54,14 @@ std::string MacAddr::to_hex(const char delimiter) const {
for (int i = 32; i >= 0; i -= 8) {
if (delimiter)
ss << delimiter;
ss << ((addr_ >> i) & 0xFF);
ss << std::setw(2) << ((addr_ >> i) & 0xFF);
}
return ss.str();
}
std::string MacAddr::str() const {
return to_hex(':');
}
std::string MacAddr::str() const { return to_hex(':'); }
std::string MacAddr::hex() const {
return to_hex();
}
std::string MacAddr::hex() const { return to_hex(); }
std::ostream &operator<<(std::ostream &out, const IpAddr &addr) {
return out << addr.str();

View File

@ -11,9 +11,9 @@ using namespace sls;
TEST_CASE("Convert mac address using classes", "[support]") {
std::vector<uint64_t> vec_addr{346856806822, 346856806852, 262027939863028};
std::vector<uint64_t> vec_addr{346856806822, 346856806852, 262027939863028,0, 281474976710655};
std::vector<std::string> vec_ans{"00:50:c2:46:d9:a6", "00:50:c2:46:d9:c4",
"ee:50:22:46:d9:f4"};
"ee:50:22:46:d9:f4", "00:00:00:00:00:00", "ff:ff:ff:ff:ff:ff"};
for (size_t i = 0; i != vec_addr.size(); ++i) {
auto mac0 = MacAddr(vec_addr[i]);
auto mac1 = MacAddr(vec_ans[i]);