Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > converting from unsigned char array to IPV6 string

Reply
Thread Tools

converting from unsigned char array to IPV6 string

 
 
sam.barker0@gmail.com
Guest
Posts: n/a
 
      04-26-2008
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()
+)),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
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      04-27-2008
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()
> +)),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:string
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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Unsigned char array to an unsigned longlong array Gabriel Genellina Python 0 08-13-2009 09:28 AM
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
forming an ipv6 address string from unsigned char array sam.barker0@gmail.com C++ 6 04-02-2008 07:48 AM
converting from IPADDRESS string to unsigned char array Obnoxious User C++ 5 04-01-2008 03:32 AM
Converting a unsigned char * to const char * hamishd C++ 12 08-05-2007 02:56 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57