forming an ipaddress string from unsigned char array

S

sam.barker0

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 convert it into a string
string address(array);
but when i store it in this way when I store 192 in the char array it
stores "-65"

Is there function like getByAddress() in C++.I am used java,but
recently I changed to c++

BTW this is not some sort of homework.I am too old for that :)
 
A

Alf P. Steinbach

* (e-mail address removed):
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 convert it into a string
string address(array);

You can convert any representation to any other representation.

but when i store it in this way when I store 192 in the char array it
stores "-65"

That cannot be the case for unsigned char. So presumably you're not using
unsigned char but just plain char.

Is there function like getByAddress() in C++.I am used java,but
recently I changed to c++

BTW this is not some sort of homework.I am too old for that :)

Presumably each char of your array1 holds one byte of the IP address.

Assuming four components,

std::string stringFromIPBytes( unsigned char ipBytes[4] )
{
std::eek:stringstream s;
for( int i = 0; i <= 3; ++i )
{
s << ipBytes;
if( i < 3 ) { s << "." }
}
return s.str();
}

or, depending on what your array is,

std::string stringFromIPBytes( char ipBytes[4] )
{
std::eek:stringstream s;
for( int i = 0; i <= 3; ++i )
{
s << static_cast<unsigned char>( ipBytes );
if( i < 3 ) { s << "." }
}
return s.str();
}

Remember to #include <string> and <sstream>. Also note that "4" is only
suggestive, a kind of documentation. It's ignored by the compiler.


Cheers, & hth,

- Alf
 
S

sam.barker0

Yep that should work.Thanks a lot Alf

* (e-mail address removed):
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 convert it into a string
string address(array);

You can convert any representation to any other representation.
but when i store it in this way when I store 192 in the char array it
stores "-65"

That cannot be the case for unsigned char. So presumably you're not using
unsigned char but just plain char.
Is there function like getByAddress() in C++.I am used java,but
recently I changed to c++
BTW this is not some sort of homework.I am too old for that :)

Presumably each char of your array1 holds one byte of the IP address.

Assuming four components,

std::string stringFromIPBytes( unsigned char ipBytes[4] )
{
std::eek:stringstream s;
for( int i = 0; i <= 3; ++i )
{
s << ipBytes;
if( i < 3 ) { s << "." }
}
return s.str();
}

or, depending on what your array is,

std::string stringFromIPBytes( char ipBytes[4] )
{
std::eek:stringstream s;
for( int i = 0; i <= 3; ++i )
{
s << static_cast<unsigned char>( ipBytes );
if( i < 3 ) { s << "." }
}
return s.str();
}

Remember to #include <string> and <sstream>. Also note that "4" is only
suggestive, a kind of documentation. It's ignored by the compiler.

Cheers, & hth,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
M

Michael.Boehnisch

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 convert it into a string
string address(array);
but when i store it in this way when I store 192 in the char array it
stores "-65"

Is there function like getByAddress() in C++.I am used java,but
recently I changed to c++

BTW this is not some sort of homework.I am too old for that :)

#include <iostream>
#include <sstream>

std::string IP2String( const unsigned char ip[4] ) {
std::eek:stringstream oss;
oss << static_cast<unsigned int>( ip[0] ) << '.'
<< static_cast<unsigned int>( ip[1] ) << '.'
<< static_cast<unsigned int>( ip[2] ) << '.'
<< static_cast<unsigned int>( ip[3] );
return oss.str();
}

int main() {
unsigned char ip[4] = { 192, 168, 0, 1 };
std::cout << IP2String( ip ) << std::endl;
return 0;
}

Consider creating a class IPAddress, instead of a member IP2String()
give it a conversion operator:

class IP {
public:
IP( const unsigned char a, const unsigned char b, const unsigned
char c, const unsigned char d )
: A( a ), B( b ), C( c ), D( d )
{}

operator std::string () const { ... } // see above

private:
unsigned char A, B, C, D;
};

This is more object oriented style. The suggest fragment allows you to
convert an IP to a std::string by casting.
E.g. std::cout << static_cast<std::string>( IP( 192, 168, 0, 2 ) ) <<
std::endl; will work.

best,

Michael
 
J

James Kanze

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 convert it into a string
string address(array);
but when i store it in this way when I store 192 in the char array it
stores "-65"
Is there function like getByAddress() in C++.I am used java,but
recently I changed to c++

Several points:

-- Anything to do with networking is system dependent. I
mostly know Posix, but from what little I've seen, Windows
is pretty similar. Under Posix, there are a number of
functions, gethostbyname, gethostbyaddr, etc. which can be
used to obtain information about a specific host.

-- At least under Posix, IP addresses have traditionally been
placed in char[4] (or char[16] for IPv6), despite the fact
that the value range for each element is 0...255, and that
char are 8 bit and signed on most implementations. This
means that if you want to actually manipulate the value in
any way (e.g. format it for display), you have to start by
explicitly converting to unsigned char. To output such an
array:

std::cout
said:
( a[ 0 ] ) )
<< '.'
said:
( a[ 1 ] ) )
<< '.'
said:
( a[ 2 ] ) )
<< '.'
said:
( a[ 3 ] ) )
;

Also, I've seen contexts where the IP address was stored as
an int (32 bits where ever I've seen it). Sometimes in
network byte order, sometime native, just to add to the
confusion. And of course, with the same problems as char,
when char is signed. Something like 192.168.1.200 will
appear as -1062731320 or -939415360 (in hex -3F57FE38 or
-37FE5740), if you just output it naïvely.

All in all, you'll probably want a class to represent IP
addresses, to wrap all of these details.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top