forming an ipaddress string from unsigned char array

Discussion in 'C++' started by sam.barker0@gmail.com, Mar 24, 2008.

  1. Guest

    Hi,
    How can I convert a unsigned char array1 which holds the ipaddress,
    into a string

    I am using a char array to insert the '.' between the bytes.
    I am using a char array because then I can convert it into a string
    string address(array);
    but when i store it in this way when I store 192 in the char array it
    stores "-65"

    Is there function like getByAddress() in C++.I am used java,but
    recently I changed to c++

    BTW this is not some sort of homework.I am too old for that :)
     
    , Mar 24, 2008
    #1
    1. Advertising

  2. * :
    > Hi,
    > How can I convert a unsigned char array1 which holds the ipaddress,
    > into a string
    >
    > I am using a char array to insert the '.' between the bytes.
    > I am using a char array because then I can convert it into a string
    > string address(array);


    You can convert any representation to any other representation.


    > but when i store it in this way when I store 192 in the char array it
    > stores "-65"


    That cannot be the case for unsigned char. So presumably you're not using
    unsigned char but just plain char.


    > Is there function like getByAddress() in C++.I am used java,but
    > recently I changed to c++
    >
    > BTW this is not some sort of homework.I am too old for that :)


    Presumably each char of your array1 holds one byte of the IP address.

    Assuming four components,

    std::string stringFromIPBytes( unsigned char ipBytes[4] )
    {
    std::eek:stringstream s;
    for( int i = 0; i <= 3; ++i )
    {
    s << ipBytes;
    if( i < 3 ) { s << "." }
    }
    return s.str();
    }

    or, depending on what your array is,

    std::string stringFromIPBytes( char ipBytes[4] )
    {
    std::eek:stringstream s;
    for( int i = 0; i <= 3; ++i )
    {
    s << static_cast<unsigned char>( ipBytes );
    if( i < 3 ) { s << "." }
    }
    return s.str();
    }

    Remember to #include <string> and <sstream>. Also note that "4" is only
    suggestive, a kind of documentation. It's ignored by the compiler.


    Cheers, & hth,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Mar 24, 2008
    #2
    1. Advertising

  3. Guest

    Yep that should work.Thanks a lot Alf

    On Mar 25, 12:40 am, "Alf P. Steinbach" <> wrote:
    > * :
    >
    > > Hi,
    > > How can I convert a unsigned char array1 which holds the ipaddress,
    > > into a string

    >
    > > I am using a char array to insert the '.' between the bytes.
    > > I am using a char array because then I can convert it into a string
    > > string address(array);

    >
    > You can convert any representation to any other representation.
    >
    > > but when i store it in this way when I store 192 in the char array it
    > > stores "-65"

    >
    > That cannot be the case for unsigned char. So presumably you're not using
    > unsigned char but just plain char.
    >
    > > Is there function like getByAddress() in C++.I am used java,but
    > > recently I changed to c++

    >
    > > BTW this is not some sort of homework.I am too old for that :)

    >
    > Presumably each char of your array1 holds one byte of the IP address.
    >
    > Assuming four components,
    >
    > std::string stringFromIPBytes( unsigned char ipBytes[4] )
    > {
    > std::eek:stringstream s;
    > for( int i = 0; i <= 3; ++i )
    > {
    > s << ipBytes;
    > if( i < 3 ) { s << "." }
    > }
    > return s.str();
    > }
    >
    > or, depending on what your array is,
    >
    > std::string stringFromIPBytes( char ipBytes[4] )
    > {
    > std::eek:stringstream s;
    > for( int i = 0; i <= 3; ++i )
    > {
    > s << static_cast<unsigned char>( ipBytes );
    > if( i < 3 ) { s << "." }
    > }
    > return s.str();
    > }
    >
    > Remember to #include <string> and <sstream>. Also note that "4" is only
    > suggestive, a kind of documentation. It's ignored by the compiler.
    >
    > Cheers, & hth,
    >
    > - Alf
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
     
    , Mar 24, 2008
    #3
  4. Guest

    On 24 Mrz., 14:18, wrote:
    > Hi,
    > How can I convert a unsigned char array1 which holds the ipaddress,
    > into a string
    >
    > I am using a char array to insert the '.' between the bytes.
    > I am using a char array because then I can convert it into a string
    > string address(array);
    > but when i store it in this way when I store 192 in the char array it
    > stores "-65"
    >
    > Is there function like getByAddress() in C++.I am used java,but
    > recently I changed to c++
    >
    > BTW this is not some sort of homework.I am too old for that :)


    #include <iostream>
    #include <sstream>

    std::string IP2String( const unsigned char ip[4] ) {
    std::eek:stringstream oss;
    oss << static_cast<unsigned int>( ip[0] ) << '.'
    << static_cast<unsigned int>( ip[1] ) << '.'
    << static_cast<unsigned int>( ip[2] ) << '.'
    << static_cast<unsigned int>( ip[3] );
    return oss.str();
    }

    int main() {
    unsigned char ip[4] = { 192, 168, 0, 1 };
    std::cout << IP2String( ip ) << std::endl;
    return 0;
    }

    Consider creating a class IPAddress, instead of a member IP2String()
    give it a conversion operator:

    class IP {
    public:
    IP( const unsigned char a, const unsigned char b, const unsigned
    char c, const unsigned char d )
    : A( a ), B( b ), C( c ), D( d )
    {}

    operator std::string () const { ... } // see above

    private:
    unsigned char A, B, C, D;
    };

    This is more object oriented style. The suggest fragment allows you to
    convert an IP to a std::string by casting.
    E.g. std::cout << static_cast<std::string>( IP( 192, 168, 0, 2 ) ) <<
    std::endl; will work.

    best,

    Michael
     
    , Mar 24, 2008
    #4
  5. James Kanze Guest

    On Mar 24, 2:18 pm, wrote:

    > How can I convert a unsigned char array1 which holds the
    > ipaddress, into a string


    > I am using a char array to insert the '.' between the bytes.
    > I am using a char array because then I can convert it into a string
    > string address(array);
    > but when i store it in this way when I store 192 in the char array it
    > stores "-65"


    > Is there function like getByAddress() in C++.I am used java,but
    > recently I changed to c++


    Several points:

    -- Anything to do with networking is system dependent. I
    mostly know Posix, but from what little I've seen, Windows
    is pretty similar. Under Posix, there are a number of
    functions, gethostbyname, gethostbyaddr, etc. which can be
    used to obtain information about a specific host.

    -- At least under Posix, IP addresses have traditionally been
    placed in char[4] (or char[16] for IPv6), despite the fact
    that the value range for each element is 0...255, and that
    char are 8 bit and signed on most implementations. This
    means that if you want to actually manipulate the value in
    any way (e.g. format it for display), you have to start by
    explicitly converting to unsigned char. To output such an
    array:

    std::cout
    << static_cast< int >( static_cast< unsigned char
    >( a[ 0 ] ) )

    << '.'
    << static_cast< int >( static_cast< unsigned char
    >( a[ 1 ] ) )

    << '.'
    << static_cast< int >( static_cast< unsigned char
    >( a[ 2 ] ) )

    << '.'
    << static_cast< int >( static_cast< unsigned char
    >( a[ 3 ] ) )

    ;

    Also, I've seen contexts where the IP address was stored as
    an int (32 bits where ever I've seen it). Sometimes in
    network byte order, sometime native, just to add to the
    confusion. And of course, with the same problems as char,
    when char is signed. Something like 192.168.1.200 will
    appear as -1062731320 or -939415360 (in hex -3F57FE38 or
    -37FE5740), if you just output it naïvely.

    All in all, you'll probably want a class to represent IP
    addresses, to wrap all of these details.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Mar 25, 2008
    #5
  6. * James Kanze:
    >
    > std::cout
    > << static_cast< int >( static_cast< unsigned char
    >> ( a[ 0 ] ) )


    Oh dang, I forgot the cast to int as well as a semicolon in the code I posted.

    Grumble.


    Cheers,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Mar 25, 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. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    617
    Jack Klein
    May 9, 2005
  2. Obnoxious User
    Replies:
    5
    Views:
    871
    Jim Langston
    Apr 1, 2008
  3. Replies:
    6
    Views:
    867
    James Kanze
    Apr 2, 2008
  4. Alex Vinokur
    Replies:
    9
    Views:
    820
    James Kanze
    Oct 13, 2008
  5. Gabriel Genellina
    Replies:
    0
    Views:
    521
    Gabriel Genellina
    Aug 13, 2009
Loading...

Share This Page