IPv4

Hi guys i just go through the one of the code which was written in functions. i tried to do in class object form but i failed to pass octets in binery function
here is mty code:

#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <math.h>

using namespace std;

class IP{


public:
string temp;
IP(){}

int getOctets(string ip, vector<int> octets){
int num;
stringstream ss(ip);
int i=0,sum=0;

while(getline(ss,temp,'.'))
octets.push_back(atoi(temp.c_str()));
return 0;
}

vector<int> maskaddress( vector<int> ip, vector<int> mask){
vector<int> output;

for(unsigned i=0; i<ip.size();i++)
output.push_back(ip[i] & mask[i]);

return output;
}

string toBinary(vector<int> octets){
string result;

for (unsigned j=0; j < octets.size(); j++)
{
if (j>0)
result += '.';

int mask = 256;
while (mask>>=1)
result += '0' + ((octets[j] & mask ) != 0);
}

return result;
}

// Convert to decimal string "nnn.nnn.nnn.nnn"
string toDecimal(vector<int> octets){
ostringstream outs;

for (unsigned j=0; j < octets.size(); j++)
{
if (j>0)
outs << '.';

outs << octets[j];
}

return outs.str();
}
};

void main(){
IP ip;
string ips="192.168.1.0";
string subnetmask="255.255.255.0";
vector<int> octetsIP;
vector<int> octetsMask;
vector<int> octetsResult;
ip.getOctets(ips,octetsIP); //sum+= (octets[i]%10) + pow(2,(double)i);
// octets[i]=(octets[i]/10);
//i++;
ip.getOctets(subnetmask,octetsMask);
octetsResult = ip.maskaddress(octetsIP, octetsMask);
cout << "ip_address: " << ip.toBinary(octetsIP) << endl;
cout << "subnetmask: " << ip.toBinary(octetsMask) << endl;
cout << "result: " << ip.toBinary(octetsResult) << endl;

cout << "\n---------------------------\n\n";

cout << "ip_address: " << ip.toDecimal(octetsIP) << endl;
cout << "subnetmask: " << ip.toDecimal(octetsMask) << endl;
cout << "result: " << ip.toDecimal(octetsResult) << endl;
}
plz reply ?????
What exactly is your error and question? And please edit your post, and repaste your code inside code brackets. They are located next to the text box where you type, and look like <>
Topic archived. No new replies allowed.