473,395 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

converting from unsigned char array to IPV6 string

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
2 4511
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
4
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is ...
8
by: Ramiro Barbosa, Jr. | last post by:
All, Any ideas on how to convert the first 8 bytes of raw uninterpreted sequence of bytes from 'char array;' (populated with _binary_ data read from a socket), into a 'long id'? Thank you! ...
15
by: Bushido Hacks | last post by:
Hey c.l.c++ and/or c.g.a.opengl posters, How do I convert a hexidecimal string, traditionally used for defining colors with HTML, into a floating point array? In other words, how do I convert...
18
by: No Such Luck | last post by:
Hi all: I have an unsigned char array (size 4): unsigned char array; array = 0x00; array = 0x00; array = 0x02; array = 0xe7;
2
by: tmr_net | last post by:
I'm using the VC++ 2005 beta. I've seen several other suggestions on the message boards, but none even compiled for me....here is my solution: args is a managed String, and the first argument...
9
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats....
0
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly, and in C# I’m using .NET Framework 2.0 (Visual...
5
by: sam.barker0 | last post by:
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:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.