Hello!

I have an ipv6 address in a string, like 2001:0db8:0000:0000:0000:0000:1428:57ab
I have to store it in two __int64.
how can i convert this?

Thx

I will try it!thx

Later, I have to calculate with this numbers.
With what kind of format will be easier?
Maybe two __int64 is not the best.
Please help, i'm beginner in this :(

Will the std::stringstream convert the portion you're working with to __int64 ?

Below is one way to break it into token...

#include <sstream>
#include <string>
#include <iostream>

int main()
{
    std::stringstream ipv6("2001:0db8:0000:0000:0000:0000:1428:57a");

    char seperator=':';

    std::string token;
    const size_t MAX_BYTE=8;
    for( size_t i=0; i < MAX_BYTE; i++ )
    {
        std::getline(ipv6,token,seperator);
        std::cout << token << std::endl;
    }
}

Thanks!

If I store it in stringstream, how can I calculate with it? like(<,>,==,+)

Below is one way to break it into token...

#include <sstream>
#include <string>
#include <iostream>

int main()
{
    std::stringstream ipv6("2001:0db8:0000:0000:0000:0000:1428:57a");

    char seperator=':';

    std::string token;
    const size_t MAX_BYTE=8;
    for( size_t i=0; i < MAX_BYTE; i++ )
    {
        std::getline(ipv6,token,seperator);
        std::cout << token << std::endl;
    }
}

Figure out how to end up with two strings, then convert as below. You might try to reassemble the tokens from above or perhaps use std::string substring/replace.

#include <iostream>
#include <stdio.h>
#include <string>

int main()
{
    std::string s1("20010db800000000");
    std::string s2("00000000142857a");

    const int BASE = 16;
    unsigned long l1 = strtoul(s1.c_str(),NULL,BASE);
    unsigned long l2 = strtoul(s2.c_str(),NULL,BASE);

    std::cout << s1 << "=" << std::hex << l1 << std::endl;
    std::cout << s2 << "=" << std::hex << l2 << std::endl;

    return 1;
}

Thanks a lot!

I will try it and then write the result!

Figure out how to end up with two strings, then convert as below. You might try to reassemble the tokens from above or perhaps use std::string substring/replace.

#include <iostream>
#include <stdio.h>
#include <string>

int main()
{
    std::string s1("20010db800000000");
    std::string s2("00000000142857a");

    const int BASE = 16;
    unsigned long l1 = strtoul(s1.c_str(),NULL,BASE);
    unsigned long l2 = strtoul(s2.c_str(),NULL,BASE);

    std::cout << s1 << "=" << std::hex << l1 << std::endl;
    std::cout << s2 << "=" << std::hex << l2 << std::endl;

    return 1;
}

Use inet_pton() in <arpa/inet.h> (Unix) or Ws2tcpip.h (Windows Vista+)
http://www.kernel.org/doc/man-pages/online/pages/man3/inet_pton.3.html
to convert the IPv6 address string and place it in a in6_addr struct. This struct has a union where the address can be viewed as an array of 16 octets.

const std::string& address_string = "fe80::202:b3ff:fe1e:8329" ;
    in6_addr address ;
    enum { NBYTES = sizeof(address._S6_un._S6_u8) } ;
    
    if( inet_pton( AF_INET6, address_string.c_str(), &address ) == 1 )
    {
        std::cout << address_string << "\nbytes (hex): " << std::hex ;
        std::for_each( address._S6_un._S6_u8, address._S6_un._S6_u8 + NBYTES,  
                       []( unsigned char byte ) { std::cout << byte << ' ' ; } ) ; 
        std::cout << '\n' << std::dec ;
    }
    else std::cout << "badly formed address string: " << address_string << '\n' ;

Hi!
I'm back!

I convert the address from: "2001:0db8::1428:57ab" format
to: "20010db80000000000000000142857ab" (string).

My question is, how can I mask this address?
So if the mask is 95, i have to write the first 95 bit to 1.
How can I do this?

Thx

I convert the string 2 __int64.
Now my question is the same: How can I mask it, if I store the address in 2 __int64?
Help please!

THX!

This article has been dead for over six months. Start a new discussion instead.