Re: how to specify power of number
- From: Antoninus Twink <nospam@xxxxxxxxxxxxxx>
- Date: Fri, 18 Apr 2008 23:58:01 +0200 (CEST)
On 18 Apr 2008 at 21:46, user923005 wrote:
If we look at his original post (and knowing he is just turning these
TCP/IP addresses into 4 byte integers):
"
numericip=atoi(textip[0])*256*256*256+atoi(textip[1])*256*256+atoi(textip[2])*256+atoi(textip[3]);"
It could clearly be done as {assuming textip[] is an array of unsigned
char} as:
numericip = (unsigned long)textip[0] << 24 +
(unsigned long)textip[1] << 16 +
(unsigned long)textip[2] << 8 +
(unsigned long)textip[3] ;
True, and someone had already suggested this. But the discussion
broadened, and someone claimed that using pow() was the best way to
raise integers to (positive) integer powers: it was this assertion that
I was responding to.
Or {better yet} simply use inet_aton() {which is also fine for
Winsock, though WSAStringToAddress() is an alternative}. That is what
would be normally done to retrieve a TCP/IP address from a dotted
address string.
HOWEVER!
The OP should keep in mind that inet_aton() does NOT support IPv6 and
that getnameinfo() should be used instead for IPv4/v6 dual stack
support.
Good advice.
To the O.P.:
You can't go wrong with "Unix Network Programming" By W. Richard
Stevens
Agreed.
.
- Follow-Ups:
- Re: how to specify power of number
- From: Dann Corbit
- Re: how to specify power of number
- References:
- how to specify power of number
- From: Yanb
- Re: how to specify power of number
- From: Bartc
- Re: how to specify power of number
- From: Antoninus Twink
- Re: how to specify power of number
- From: Yanb
- Re: how to specify power of number
- From: Antoninus Twink
- Re: how to specify power of number
- From: user923005
- how to specify power of number
- Prev by Date: Re: how to specify power of number
- Next by Date: Can a double be 32 bits?
- Previous by thread: Re: how to specify power of number
- Next by thread: Re: how to specify power of number
- Index(es):
Relevant Pages
|