client bug fix: smaller ips convert to hex

This commit is contained in:
maliakal_d 2019-05-16 14:07:58 +02:00
parent f1a1391866
commit 9315768159
2 changed files with 9 additions and 4 deletions

View File

@ -31,9 +31,9 @@ std::string IpAddr::str() const {
}
std::string IpAddr::hex() const {
std::ostringstream ss;
ss << std::hex << std::setfill('0') << std::setw(2);
for (int i = 0; i != 4; ++i) {
ss << ((addr_ >> i * 8) & 0xFF);
ss << std::hex << std::setfill('0') << std::setw(2)
<< ((addr_ >> i * 8) & 0xFF);
}
return ss.str();
}

View File

@ -42,9 +42,12 @@ TEST_CASE("Hex representation of MAC", "[support]") {
}
TEST_CASE("Convert IP using classes ", "[support]") {
std::vector<uint32_t> vec_addr{4073554305, 2747957633, 2697625985};
std::vector<uint32_t> vec_addr{4073554305, 2747957633, 2697625985, 2566979594, 0};
std::vector<std::string> vec_ans{"129.129.205.242", "129.129.202.163",
"129.129.202.160"};
"129.129.202.160", "10.0.1.153", "0.0.0.0"};
std::vector<std::string> vec_hex{"8181cdf2", "8181caa3",
"8181caa0", "0a000199","00000000"};
for (size_t i = 0; i != vec_addr.size(); ++i) {
auto ip0 = IpAddr(vec_addr[i]);
@ -57,6 +60,8 @@ TEST_CASE("Convert IP using classes ", "[support]") {
CHECK(ip1 == vec_ans[i]);
CHECK(ip0.str() == vec_ans[i]);
CHECK(ip1.str() == vec_ans[i]);
CHECK(ip0.hex() == vec_hex[i]);
CHECK(ip1.hex() == vec_hex[i]);
}
}