473,390 Members | 1,414 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,390 software developers and data experts.

forming an ipv6 address string from unsigned char array

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
5 7301
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
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

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

Similar topics

7
by: AlexFarokhyans | last post by:
Hello, I'm trying to convert String to Char array. I'm getting a string from user input text box and then I have char firstName. I need to convert the string that is in the text box to firstName....
2
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in...
1
by: vsk | last post by:
Hai, I need to know how to convert an unsigned char array into hexstring. can anyone help me in this regard?. Thanks.
1
by: joye | last post by:
Hello, How to convert an unmanaged string with char array type to a managed string with char array type? Thanks. Regards, Tsung-Yu
0
by: sam.barker0 | last post by:
Hi, How can I convert a unsigned char array1 which holds the ipaddress, into a string I am using a char array to insert the '.' between the bytes. I am using a char array because then I can...
2
by: sam.barker0 | last post by:
Hi , I am trying to convert from an IPADDRESS string to a unsigned char array I tried to use c_str().Its was stupid because I tried to cast it with <unsigned int. Is there a way easily do...
2
by: sam.barker0 | last post by:
Hi guys, I am trying to form an IPV6 address string from the address bytes contained in a unsigned char buffer char tempstring; sprintf(tempstring, "%x:%x:%x:%x:%x:%x:%x:%x",htons(*((unsigned...
2
by: ARAVINDRNR | last post by:
How to change an unsigned char array to bit map file in 'C' langauge
10
by: Gary Rubin | last post by:
I am trying to figure out how to get the contents of a textbox text property into an unsigned char array. I have been researching this via google for a few days without any success unfortunately. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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.