473,320 Members | 1,854 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,320 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 7282
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.