Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > forming an ipaddress string from unsigned char array

Reply
Thread Tools

forming an ipaddress string from unsigned char array

 
 
sam.barker0@gmail.com
Guest
Posts: n/a
 
      03-24-2008
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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-24-2008
* http://www.velocityreviews.com/forums/(E-Mail 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:stringstream s;
for( int i = 0; i <= 3; ++i )
{
s << ipBytes[i];
if( i < 3 ) { s << "." }
}
return s.str();
}

or, depending on what your array is,

std::string stringFromIPBytes( char ipBytes[4] )
{
std:stringstream s;
for( int i = 0; i <= 3; ++i )
{
s << static_cast<unsigned char>( ipBytes[i] );
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?
 
Reply With Quote
 
 
 
 
sam.barker0@gmail.com
Guest
Posts: n/a
 
      03-24-2008

Yep that should work.Thanks a lot Alf

On Mar 25, 12:40 am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * (E-Mail 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:stringstream s;
> for( int i = 0; i <= 3; ++i )
> {
> s << ipBytes[i];
> if( i < 3 ) { s << "." }
> }
> return s.str();
> }
>
> or, depending on what your array is,
>
> std::string stringFromIPBytes( char ipBytes[4] )
> {
> std:stringstream s;
> for( int i = 0; i <= 3; ++i )
> {
> s << static_cast<unsigned char>( ipBytes[i] );
> 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?


 
Reply With Quote
 
Michael.Boehnisch@gmail.com
Guest
Posts: n/a
 
      03-24-2008
On 24 Mrz., 14:18, (E-Mail Removed) wrote:
> 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: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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-25-2008
On Mar 24, 2:18 pm, (E-Mail Removed) wrote:

> 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
<< static_cast< int >( static_cast< unsigned char
>( a[ 0 ] ) )

<< '.'
<< static_cast< int >( static_cast< unsigned char
>( a[ 1 ] ) )

<< '.'
<< static_cast< int >( static_cast< unsigned char
>( a[ 2 ] ) )

<< '.'
<< static_cast< int >( static_cast< unsigned char
>( 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.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-25-2008
* James Kanze:
>
> std::cout
> << static_cast< int >( static_cast< unsigned char
>> ( a[ 0 ] ) )


Oh dang, I forgot the cast to int as well as a semicolon in the code I posted.

Grumble.


Cheers,

- 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Unsigned char array to an unsigned longlong array Gabriel Genellina Python 0 08-13-2009 09:28 AM
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
forming an ipv6 address string from unsigned char array sam.barker0@gmail.com C++ 6 04-02-2008 07:48 AM
converting from IPADDRESS string to unsigned char array Obnoxious User C++ 5 04-01-2008 03:32 AM
void*, char*, unsigned char*, signed char* Steffen Fiksdal C Programming 1 05-09-2005 02:33 AM



Advertisments