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

converting from unsigned char array to IPV6 string

P: n/a
Hi guys,
I am trying to form an IPV6 address string from the address bytes
contained in a unsigned char buffer

char tempstring[35];
sprintf(tempstring, "%x:%x:%x:%x:%x:%x:%x:%x",htons(*((unsigned short
*)(buf.GetStart()))),htons(*((unsigned short *)(buf.GetStart()
+2))),htons(*((unsigned short *)(buf.GetStart()+4))),htons(*((unsigned
short *)(buf.GetStart()+6))),htons(*((unsigned short *)(buf.GetStart()
+8))),htons(*((unsigned short *)(buf.GetStart()
+10))),htons(*((unsigned short *)(buf.GetStart()
+12))),htons(*((unsigned short *)(buf.GetStart()+14))))

There is a stack over flow when I do this.Its becase tempstring is
char instead of unsigned char.
But sprintf allows only char array.

How can I solve the problem.Is there a better way to write this?
Cheers,
Sam
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Sat, 26 Apr 2008 03:46:11 -0700 (PDT) in comp.lang.c++,
sa*********@gmail.com wrote,
>There is a stack over flow when I do this.Its becase tempstring is
char instead of unsigned char.
That's the last reason it could be. Perhaps it is because you are
putting 40 chars in a 35 char buffer?
>How can I solve the problem.Is there a better way to write this?
How about std::ostringstream?
Jun 27 '08 #2

P: n/a
sa*********@gmail.com wrote:
Hi guys,
I am trying to form an IPV6 address string from the address bytes
contained in a unsigned char buffer

char tempstring[35];
Becuase you're getting stack overflow, 35 is probably not enough characters.
to test this make the size some rediculously large value, like 100, 1000,
run the code. look at the output.
sprintf(tempstring, "%x:%x:%x:%x:%x:%x:%x:%x",htons(*((unsigned short
*)(buf.GetStart()))),htons(*((unsigned short *)(buf.GetStart()
+2))),htons(*((unsigned short *)(buf.GetStart()+4))),htons(*((unsigned
short *)(buf.GetStart()+6))),htons(*((unsigned short *)(buf.GetStart()
+8))),htons(*((unsigned short *)(buf.GetStart()
+10))),htons(*((unsigned short *)(buf.GetStart()
+12))),htons(*((unsigned short *)(buf.GetStart()+14))))

There is a stack over flow when I do this.Its becase tempstring is
char instead of unsigned char.
But sprintf allows only char array.

How can I solve the problem.Is there a better way to write this?
std::string +
std::ostring
std::iostring

Anything that has a dynamic buffer that will grow with the size so you don't
have to guess how big to make it.

I don't know the format of IP6, but notice you are using unsidned short,
which is on my system 2 bytes. which goes from 0 to 65535. At their
largest, 5 * 8 = 40 plus null terminator is 41 characters, which 35 isn't.
I really don't know though. stack overflow type problems are usually from
buffer overflows, especially when you are using a buffer like you are here.

--
Jim Langston
ta*******@rocketmail.com
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.