converting from IPADDRESS string to unsigned char array

O

Obnoxious User

Hi ,
I am trying to convert from an IPADDRESS string [say "12.12.1.2"]to a
unsigned char array[containing the octets witout the dots]

I tried to use c_str().Its was stupid because I tried to cast it with
<unsigned int> .
Is there a way easily do this.

#include <sstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

std::istream & operator>>(std::stringstream & strm, std::vector<int> & v) {
if(strm.good()) {
int temp = ~0;
strm >> temp;
v.push_back(temp);
}
return strm;
}

int main() {
std::stringstream stream("12.12.1.12");
std::vector<int> v;
while(stream.good()) {
stream >> v;
stream.ignore();
}
std::copy(v.begin(),v.end(),std::eek:stream_iterator<int>(std::cout,"\n"));
return 0;
}
 
S

sam.barker0

Hi ,
I am trying to convert from an IPADDRESS string [say "12.12.1.2"]to a
unsigned char array[containing the octets witout the dots]

I tried to use c_str().Its was stupid because I tried to cast it with
<unsigned int> .
Is there a way easily do this.

Cheers,
Sam
 
S

sam.barker0

Hi,
Iam using the above code in a class
in the .cc file


std::istream & myclass::eek:perator>>(std::stringstream & strm,
std::vector<int> & v) {
if(strm.good()) {
int temp = ~0;
strm >> temp;
v.push_back(temp);
}
return strm;

}
myclass::getIP(string str)
{
std::stringstream stream("12.12.1.12");
std::vector<int> v;
while(stream.good()) {
stream >> v;
stream.ignore();
}

std::copy(v.begin(),v.end(),std::eek:stream_iterator<int>(std::cout,"\n"));

}

I am getting the error
error: 'std::istream& myclass::eek:perator>>(std::stringstream&,
std::vector<int, std::allocator<int> >&)' must take exactly one
argument

I do understand what it means but i dont know how to solve it.
How does it work when I just copy the code intoa file and compile it
 
O

Obnoxious User

Hi,
Iam using the above code in a class
in the .cc file


std::istream & myclass::eek:perator>>(std::stringstream & strm,
std::vector<int> & v) {
if(strm.good()) {
int temp = ~0;
strm >> temp;
v.push_back(temp);
}
return strm;

}
myclass::getIP(string str)
{
std::stringstream stream("12.12.1.12");
std::vector<int> v;
while(stream.good()) {
stream >> v;
stream.ignore();
}

std::copy(v.begin(),v.end(),std::eek:stream_iterator<int>(std::cout,"\n"));

}

I am getting the error
error: 'std::istream& myclass::eek:perator>>(std::stringstream&,
std::vector<int, std::allocator<int> >&)' must take exactly one argument

I do understand what it means but i dont know how to solve it. How does
it work when I just copy the code intoa file and compile it

Compare what I wrote and what you wrote with these examples:

1)----------------------------------------------------------

class A {
public:
int d_var;
A(int v) : d_var(v) {}
};

void operator>>(A const & a, A & b) {
b.d_var = a.d_var;
}

int main() {
A d(1),c(0);
d >> c;
return 0;
}

2)----------------------------------------------------------

class A {
public:
int d_var;
A(int v) : d_var(v) {}
void operator>>(A & b) {
b.d_var = d_var;
}
};

int main() {
A d(8),c(4);
d >> c;
return 0;
}
 
J

Jim Langston

Hi,
Iam using the above code in a class
in the .cc file


std::istream & myclass::eek:perator>>(std::stringstream & strm,
std::vector<int> & v) {
if(strm.good()) {
int temp = ~0;
strm >> temp;
v.push_back(temp);
}
return strm;

}
myclass::getIP(string str)
{
std::stringstream stream("12.12.1.12");
std::vector<int> v;
while(stream.good()) {
stream >> v;
stream.ignore();
}

std::copy(v.begin(),v.end(),std::eek:stream_iterator<int>(std::cout,"\n"));

}

I am getting the error
error: 'std::istream& myclass::eek:perator>>(std::stringstream&,
std::vector<int, std::allocator<int> >&)' must take exactly one
argument

I do understand what it means but i dont know how to solve it.
How does it work when I just copy the code intoa file and compile it

Also, look for the function gethostbyname()

It pretty much does it for you, That's in windows though, not sure what it's
called in Linux it might be the same. Linux and Windows socket code is very
similar.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top