Advertisement
pablo7890

ip v2 broadcast calc

Mar 20th, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <cstdio>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <cstring>
  8.  
  9. using namespace std;
  10.  
  11. int BinToDec(const char *binary)
  12. {
  13.     int len, dec = 0, i, exp;
  14.  
  15.     len = strlen(binary);
  16.     exp = len - 1;
  17.  
  18.     for (i = 0; i < len; i++, exp--)
  19.         dec += binary[i] == '1' ? pow(2, exp) : 0;
  20.     return dec;
  21. }
  22.  
  23. string DecToBin(int decimal)
  24. {
  25.     return decimal ==
  26.         0 ? "" : DecToBin(decimal / 2) + ((decimal % 2) ? "1" : "0");
  27. }
  28.  
  29. int StrToInt(string str)
  30. {
  31.     int i = atoi(str.c_str());
  32.     return i;
  33. }
  34.  
  35. int Addr[4];
  36. void ConvertFromAddr(string ip)
  37. {
  38.     stringstream s(ip);
  39.     int a, b, c, d;
  40.     char ch;
  41.     s >> a >> ch >> b >> ch >> c >> ch >> d;
  42.  
  43.     Addr[0] = a;
  44.     Addr[1] = b;
  45.     Addr[2] = c;
  46.     Addr[3] = d;
  47. }
  48.  
  49. int main()
  50. {
  51.     string ip, mask;
  52.     cin >> ip >> mask;
  53.     // IP start
  54.     ConvertFromAddr(ip); // del dots and array address
  55.     string binIp[4];
  56.     int binIntIp[4];
  57.     for (int i = 0; i<4; i++)
  58.     {
  59.         // fill missing zeros
  60.         int lengthAddr = floor(log10(abs(Addr[i]))) + 1;
  61.         if (lengthAddr<8)
  62.         {
  63.             for (int j=0; j<8-lengthAddr;j++)
  64.                 binIp[i] += "0";
  65.         }
  66.         // add binary value of certain 8 bits
  67.         binIp[i] += DecToBin(Addr[i]);
  68.         // convert string to int
  69.         binIntIp[i] = StrToInt(binIp[i]);
  70.     }
  71.     int arrayBinIp[4][8];
  72.     for (int j=0; j<4; j++)
  73.     {
  74.         for (int i = 7; i >= 0; i--)
  75.         {
  76.             // every number goes to its field
  77.             arrayBinIp[j][i] = binIntIp[j] % 10;
  78.             binIntIp[j] /= 10;
  79.         }
  80.     }
  81.     // IP end
  82.     // mask start
  83.     ConvertFromAddr(mask);
  84.     string binMask[4];
  85.     int binIntMask[4];
  86.     for (int i = 0; i<4; i++)
  87.     {
  88.         int lengthAddr = floor(log10(abs(Addr[i]))) + 1;
  89.         if (lengthAddr<8)
  90.         {
  91.             for (int j=0; j<8-lengthAddr;j++)
  92.                 binMask[i] += "0";
  93.         }
  94.         binMask[i] += DecToBin(Addr[i]);
  95.         binIntMask[i] = StrToInt(binMask[i]);
  96.     }
  97.     int arrayBinMask[4][8];
  98.     for (int j=0; j<4; j++)
  99.     {
  100.         for (int i = 7; i >= 0; i--)
  101.         {
  102.             arrayBinMask[j][i] = binIntMask[j] % 10;
  103.             binIntMask[j] /= 10;
  104.         }
  105.     }
  106.     // mask end
  107.     // let's compare them...
  108.     for (int i=0;i<4;i++)
  109.     {
  110.      for (int j=0;j<8;j++)
  111.      {
  112.        if (arrayBinMask[i][j]==0)
  113.        {
  114.          // arrayBinIp became the broadcast address
  115.         arrayBinIp[i][j]=1;
  116.        }
  117.      }
  118.     }
  119.     string binBroadcast[4];
  120.     int Broadcast[4];
  121.     for (int i = 0;i<4;i++)
  122.     {
  123.      for (int j =0;j<8; j++)
  124.      {
  125.        binBroadcast[i] += arrayBinIp[i][j]+48;
  126.      }
  127.  
  128.     char *cstr = new char[binBroadcast[i].length() + 1];
  129. strcpy(cstr, binBroadcast[i].c_str());
  130.  
  131.   Broadcast[i]=BinToDec(cstr);
  132.     }
  133.    
  134.     cout << Broadcast[0] << "." <<  Broadcast[1] << "." <<  Broadcast[2] << "." <<  Broadcast[3];
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement