converting from IPADDRESS string to unsigned char array

Discussion in 'C++' started by Obnoxious User, Mar 30, 2008.

  1. On Sun, 30 Mar 2008 05:32:28 -0700, sam.barker0 wrote:

    > 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;
    }

    --
    OU
    Obnoxious User, Mar 30, 2008
    #1
    1. Advertising

  2. Obnoxious User

    Guest

    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
    , Mar 30, 2008
    #2
    1. Advertising

  3. Obnoxious User

    Guest

    Hi,
    Thanks for the reply.But
    I need the result in an unsigned char array.
    , Mar 30, 2008
    #3
  4. Obnoxious User

    Guest

    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
    , Mar 31, 2008
    #4
  5. On Mon, 31 Mar 2008 04:14:35 -0700, sam.barker0 wrote:

    > 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;
    }

    --
    OU
    Obnoxious User, Mar 31, 2008
    #5
  6. Obnoxious User

    Jim Langston Guest

    wrote:
    > 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.


    --
    Jim Langston
    Jim Langston, Apr 1, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hamishd
    Replies:
    12
    Views:
    4,991
  2. Replies:
    5
    Views:
    1,247
    Alf P. Steinbach
    Mar 25, 2008
  3. Replies:
    1
    Views:
    773
    Jim Langston
    Apr 27, 2008
  4. Alex Vinokur
    Replies:
    9
    Views:
    773
    James Kanze
    Oct 13, 2008
  5. Gabriel Genellina
    Replies:
    0
    Views:
    506
    Gabriel Genellina
    Aug 13, 2009
Loading...

Share This Page