forming an ipv6 address string from unsigned char array

Discussion in 'C++' started by sam.barker0@gmail.com, Apr 1, 2008.

  1. Guest

    Hi,
    How can I convert an unsigned char array containing IPV6 address into
    a string
    Eg
    if arrray contains
    20 01 05 03 a8 3e 00 00 00 00 00 00 00 02 00 30

    Then the address is
    Addr: 2001:0503:a83e:0000:0000:0000:0002:0030

    I tried to do write like below but then its completely wrong.Is there
    any cast for hex
    for(int i=0;i<=15;i++)
    {
    oss << static_cast<unsigned int>(*(Rec.GetStart()+i));

    Cheers,
    Sam
     
    , Apr 1, 2008
    #1
    1. Advertising

  2. Oncaphillis Guest

    wrote:
    > Hi,
    > How can I convert an unsigned char array containing IPV6 address into
    > a string
    > Eg
    > if arrray contains
    > 20 01 05 03 a8 3e 00 00 00 00 00 00 00 02 00 30
    >
    > Then the address is
    > Addr: 2001:0503:a83e:0000:0000:0000:0002:0030
    >
    > I tried to do write like below but then its completely wrong.Is there
    > any cast for hex
    > for(int i=0;i<=15;i++)
    > {
    > oss << static_cast<unsigned int>(*(Rec.GetStart()+i));
    >
    > Cheers,
    > Sam


    How's about this ?

    <snip>

    #include <iostream>
    #include <iomanip>
    #include <sstream>

    const unsigned char * cs =
    (const unsigned char *)
    "20 01 05 03 a8 3e 00 00 00 00 00 00 00 02 00 30";

    int main() {
    std::stringstream is((const char*)cs);
    std::stringstream os;

    int i;

    for(i=0;i<16 && is;i++) {
    unsigned int n;

    is >> std::hex >> n;

    if(!is || n > 0xff) {
    std::cerr << "failed to read (valid) number";
    return -1;
    }

    os << (i % 2 || i==0 ? "" : ":")
    << std::hex << std::setw(2) << std::setfill('0') << n;
    }

    if(i!=16) {
    std::cerr << "failed to eat up string" << std::endl;
    return -1;
    }

    std::cerr << os.str() << std::endl;
    }

    </snip>

    Hope that helps

    o.
     
    Oncaphillis, Apr 1, 2008
    #2
    1. Advertising

  3. Guest

    Hi,
    I am sorry.I made a mistake.

    arrray contains
    32 01 05 03 168 62 00 00 00 00 00 00 00 02 00 48

    Then the address is
    Addr: 2001:0503:a83e:0000:0000:0000:0002:0030

    The array is unsigned char type
     
    , Apr 1, 2008
    #3
  4. Guest

    Hi,
    I have come up with the solution like this.
    sprintf(tempstring, "%x:%x:%x:%x:%x:%x:%x:%x",htons(*((unsigned short
    *)(Rec))),htons(*((unsigned short *)(Rec+2))),htons(*((unsigned short
    *)(Rec+4))),htons(*((unsigned short *)(Rec+6))),htons(*((unsigned
    short *)(Rec+8))),htons(*((unsigned short *)(Rec
    +10))),htons(*((unsigned short *)(Rec+12))),htons(*((unsigned short *)
    (Rec+14))));

    Is there a better looking solution.
    CHeers
     
    , Apr 1, 2008
    #4
  5. Oncaphillis Guest

    wrote:
    > Hi,
    > I am sorry.I made a mistake.
    >
    > arrray contains
    > 32 01 05 03 168 62 00 00 00 00 00 00 00 02 00 48
    >
    > Then the address is
    > Addr: 2001:0503:a83e:0000:0000:0000:0002:0030
    >
    > The array is unsigned char type


    Hmm.. your first request wasn't very detailed.
    So it's an array of unsigned char (which serve
    as a octet here) which hold the ipv6 address.
    NOT an array of char which hold the address
    (encoded in *ASCII*).

    So it seems like your working on the in6_addr
    struct.

    And you're mixing up decimal and hex representation
    here. So 32 is supposed to mean 0x20 and
    168 == 0xa8 and 62 == 0x3e etc.

    O.
     
    Oncaphillis, Apr 1, 2008
    #5
  6. Oncaphillis Guest

    wrote:
    > Hi,
    > I have come up with the solution like this.
    > sprintf(tempstring, "%x:%x:%x:%x:%x:%x:%x:%x",htons(*((unsigned short
    > *)(Rec))),htons(*((unsigned short *)(Rec+2))),htons(*((unsigned short
    > *)(Rec+4))),htons(*((unsigned short *)(Rec+6))),htons(*((unsigned
    > short *)(Rec+8))),htons(*((unsigned short *)(Rec
    > +10))),htons(*((unsigned short *)(Rec+12))),htons(*((unsigned short *)
    > (Rec+14))));
    >
    > Is there a better looking solution.
    > CHeers


    have a look at

    http://www.opengroup.org/onlinepubs/000095399/functions/inet_ntop.html

    I guess it solves your problem in a portable way.

    Although it doesn't have to do with C++ ;-)

    O.
     
    Oncaphillis, Apr 1, 2008
    #6
  7. James Kanze Guest

    On Apr 1, 1:57 pm, wrote:

    > How can I convert an unsigned char array containing IPV6
    > address into a string
    > Eg
    > if arrray contains
    > 20 01 05 03 a8 3e 00 00 00 00 00 00 00 02 00 30


    > Then the address is
    > Addr: 2001:0503:a83e:0000:0000:0000:0002:0030


    > I tried to do write like below but then its completely wrong.Is there
    > any cast for hex
    > for(int i=0;i<=15;i++)
    > {
    > oss << static_cast<unsigned int>(*(Rec.GetStart()+i));


    I'd do something like:

    std::string
    asIPv6String(
    unsigned char const* source )
    {
    std::eek:stringstream result ;
    result.setf( std::ios::hex, std::ios::basefield ) ;
    result.fill( '0' ) ;
    for ( int i = 0 ; i < 16 ; i += 2 ) {
    result << ':'
    << std::setw( 4 )
    << 256 * source[ i ] + source[ i + 1 ] ;
    }
    return result.str().substr( 1 ) ;
    }

    (More likely, of course, I'd integrate this into a << operator
    for an IPv6 class.)

    --
    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, Apr 2, 2008
    #7
    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. Replies:
    5
    Views:
    1,273
    Alf P. Steinbach
    Mar 25, 2008
  2. Replies:
    1
    Views:
    791
    Jim Langston
    Apr 27, 2008
  3. Alex Vinokur
    Replies:
    9
    Views:
    799
    James Kanze
    Oct 13, 2008
  4. Gabriel Genellina
    Replies:
    0
    Views:
    516
    Gabriel Genellina
    Aug 13, 2009
  5. Jesse van den Kieboom

    Ipv6 on a ipv4/ipv6 hostname

    Jesse van den Kieboom, Jun 5, 2005, in forum: Ruby
    Replies:
    1
    Views:
    364
    Dibya Prakash
    Jun 5, 2005
Loading...

Share This Page