---
# System prepended metadata

title: 'int32 to IPv4 [5 kyu]'
tags: [C++, CodeWars]

---

# int32 to IPv4 [5 kyu]

[int32 to IPv4](https://www.codewars.com/kata/52e88b39ffb6ac53a400022e)
5 kyu

```cpp=
/**
*** Author: R-CO
*** E-Mail: daniel1820kobe@gmail.com
*** Date: 2020-08-12
**/
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;
#include <string>

struct TestCaseStruct {
  uint32_t input;
  std::string expected_output;
};

class Solution {
 public:
  std::string uint32_to_ip(uint32_t ip) {
    const uint32_t kParts = 4;
    uint32_t part[kParts];
    for (uint32_t i = 0; i < kParts; ++i) {
      part[i] = ip & 0xFF;
      ip >>= 8;
    }

    std::string output = std::to_string(part[3]) + "." + std::to_string(part[2]) + "." + std::to_string(part[1]) + "." + std::to_string(part[0]);
    return output;
  }
};

int main(int argc, char *argv[]) {
  /*
    It(sample_tests)
    {
        Assert::That(uint32_to_ip(2154959208), Equals("128.114.17.104"));
        Assert::That(uint32_to_ip(0), Equals("0.0.0.0"));
        Assert::That(uint32_to_ip(2149583361), Equals("128.32.10.1"));
    }
   */
  TestCaseStruct test_cases[] = {{2154959208, "128.114.17.104"},
                                 {0, "0.0.0.0"},
                                 {2149583361, "128.32.10.1"}};

  Solution solution;
  for (const auto &test_case : test_cases) {
    if (solution.uint32_to_ip(test_case.input) == test_case.expected_output) {
      cout << "test case \"" << test_case.input << "\" is pass." << std::endl;
    } else {
      cout << "test case \"" << test_case.input << "\" is fail." << std::endl;
    }
  }

  return EXIT_SUCCESS;
}

```

## Result
![](https://i.imgur.com/Dw7AvHs.png)


###### tags: `CodeWars` `C++`
