By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,634 Members | 1,897 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,634 IT Pros & Developers. It's quick & easy.

forming an ipv6 address string from unsigned char array

P: n/a
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 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
sa*********@gmail.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<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.
Apr 1 '08 #2

P: n/a
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 '08 #3

P: n/a

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 '08 #4

P: n/a
sa*********@gmail.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.
Apr 1 '08 #5

P: n/a
sa*********@gmail.com 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/...inet_ntop.html

I guess it solves your problem in a portable way.

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

O.
Apr 1 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.