forming an ipv6 address string from unsigned char array

S

sam.barker0

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
 
O

Oncaphillis

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.
 
S

sam.barker0

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
 
S

sam.barker0

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
 
O

Oncaphillis

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.
 
O

Oncaphillis

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.
 
J

James Kanze

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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top