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 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.
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
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 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. 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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....
|
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...
|
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.
|
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
|
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...
|
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...
|
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...
|
by: ARAVINDRNR |
last post by:
How to change an unsigned char array to bit map file in 'C' langauge
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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....
| |