std::string addrToBinary(std::array<uint8_t, 4>& addr)
{
std::string strAddr = "";
size_t i;
for (i = 0; i < addr.size() - 1; i++)
strAddr += std::to_string(addr.at(i)) + '.';
strAddr += std::to_string(addr.at(i));
// delete '.'
std::array<int, 4> Numip;
for (int i = 0; i < 4; i++) {
Numip.at(i) = stoi(strAddr.substr(0, strAddr.find('.')));
std::string tostr = std::to_string(Numip[i]);
strAddr.erase(0, tostr.length() + 1);
}
// num to binnum //last 0 faild
for (int i = 0; i < 4; i++) {
while (Numip.at(i) > 0) {
strAddr += char('0' + Numip.at(i) % 2);
Numip.at(i) = Numip.at(i) / 2;
}
strAddr += ".";
}
// reverse and add 0 to string and cout
// last 1 faild
std::string s;
size_t n = 8;
for (size_t i = 0; i < strAddr.size(); i++) {
std::string Binip = strAddr.substr(0, strAddr.find('.'));
std::reverse(Binip.begin(), Binip.end());
int precision = n - std::min(n, Binip.size());
s = std::string(precision, '0').append(Binip);
std::cout << s << " ";
strAddr.erase(0, Binip.length() + 1);
}
return "";
}
int main()
{
std::array<uint8_t, 4> m_address = {192, 168, 1, 1};
addrToBinary(m_address);
return 0;
}
std::string addrToBinary(const std::array<uint8_t, 4>& addr) const
{
std::string strAddr = "";
size_t i;
for (i = 0; i < addr.size() - 1; i++)
strAddr += std::to_string(addr.at(i)) + '.';
strAddr += std::to_string(addr.at(i));
// delete '.'
std::array<int, 4> Numip;
for (int i = 0; i < 4; i++) {
Numip.at(i) = stoi(strAddr.substr(0, strAddr.find('.')));
std::string tostr = std::to_string(Numip[i]);
strAddr.erase(0, tostr.length() + 1);
}
// num to binnum //last 0 faild
for (int i = 0; i < 4; i++) {
while (Numip.at(i) > 0) {
strAddr += char('0' + Numip.at(i) % 2);
Numip.at(i) = Numip.at(i) / 2;
}
strAddr += ".";
}
// reverse and add 0 to string and cout
// last 1 faild
std::string s;
size_t n = 8;
for (size_t i = 0; i < strAddr.size(); i++) {
std::string Binip = strAddr.substr(0, strAddr.find('.'));
std::reverse(Binip.begin(), Binip.end());
int precision = n - std::min(n, Binip.size());
s = std::string(precision, '0').append(Binip);
std::cout << s << " ";
strAddr.erase(0, Binip.length() + 1);
}
return "";
}
// num to binnum //last 0 faild
for (int i = 0; i < 4; i++) {
while (Numip.at(i) > 0) {
strAddr += char('0' + Numip.at(i) % 2);
Numip.at(i) = Numip.at(i) / 2;
}
strAddr += ".";
}
// reverse and add 0 to string and cout
// last 1 faild
std::string s;
size_t n = 8;
for (size_t i = 0; i < strAddr.size(); i++) {
std::string Binip = strAddr.substr(0, strAddr.find('.'));
std::reverse(Binip.begin(), Binip.end());
int precision = n - std::min(n, Binip.size());
s = std::string(precision, '0').append(Binip);
std::cout << s << " ";
strAddr.erase(0, Binip.length() + 1);
}
//допустим ввод 255.255.255.0
std::string s;
for (size_t i = 0; i < strAddr.size(); i++) {
std::string Binip = strAddr.substr(0, strAddr.find(' '));
std::reverse(Binip.begin(), Binip.end());
int precision = n - std::min(n, Binip.size());
s = std::string(precision, '0').append(Binip);
std::cout << s << " "; // вывод 11111111 11111111 11111111 00000000
strAddr.erase(0, Binip.length() + 1);
}
std::cout << s << " "; // вывод 00000000
for (size_t i = 0; i < strAddr.size(); i++) {
std::string Binip = strAddr.substr(0, strAddr.find(' '));
std::reverse(Binip.begin(), Binip.end());
int precision = n - std::min(n, Binip.size());
std::string s = std::string(precision, '0').append(Binip);
std::cout << s << " ";
strAddr.erase(0, Binip.length() + 1);
}