forming an ipv6 address string from unsigned char array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sam.barker0@gmail.com

    forming an ipv6 address string from unsigned char array

    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<uns igned int>(*(Rec.GetS tart()+i));

    Cheers,
    Sam
  • Oncaphillis

    #2
    Re: forming an ipv6 address string from unsigned char array

    sam.barker0@gma il.com 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<uns igned int>(*(Rec.GetS tart()+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::stringstre am is((const char*)cs);
    std::stringstre am 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.

    Comment

    • sam.barker0@gmail.com

      #3
      Re: forming an ipv6 address string from unsigned char array

      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

      Comment

      • sam.barker0@gmail.com

        #4
        Re: forming an ipv6 address string from unsigned char array


        Hi,
        I have come up with the solution like this.
        sprintf(tempstr ing, "%x:%x:%x:%x:%x :%x:%x:%x",hton s(*((unsigned short
        *)(Rec))),htons (*((unsigned short *)(Rec+2))),hto ns(*((unsigned short
        *)(Rec+4))),hto ns(*((unsigned short *)(Rec+6))),hto ns(*((unsigned
        short *)(Rec+8))),hto ns(*((unsigned short *)(Rec
        +10))),htons(*( (unsigned short *)(Rec+12))),ht ons(*((unsigned short *)
        (Rec+14))));

        Is there a better looking solution.
        CHeers

        Comment

        • Oncaphillis

          #5
          Re: forming an ipv6 address string from unsigned char array

          sam.barker0@gma il.com 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.


          Comment

          • Oncaphillis

            #6
            Re: forming an ipv6 address string from unsigned char array

            sam.barker0@gma il.com wrote:
            Hi,
            I have come up with the solution like this.
            sprintf(tempstr ing, "%x:%x:%x:%x:%x :%x:%x:%x",hton s(*((unsigned short
            *)(Rec))),htons (*((unsigned short *)(Rec+2))),hto ns(*((unsigned short
            *)(Rec+4))),hto ns(*((unsigned short *)(Rec+6))),hto ns(*((unsigned
            short *)(Rec+8))),hto ns(*((unsigned short *)(Rec
            +10))),htons(*( (unsigned short *)(Rec+12))),ht ons(*((unsigned short *)
            (Rec+14))));
            >
            Is there a better looking solution.
            CHeers
            have a look at



            I guess it solves your problem in a portable way.

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

            O.

            Comment

            Working...