[107178] in North American Network Operators' Group

home help back first fref pref prev next nref lref last post

Re: interger to I P address

daemon@ATHENA.MIT.EDU (Owen DeLong)
Wed Aug 27 15:39:08 2008

From: Owen DeLong <owen@delong.com>
To: Robert Kisteleki <robert@ripe.net>
In-Reply-To: <48B589C9.4040006@ripe.net>
Date: Wed, 27 Aug 2008 12:38:47 -0700
Cc: NANOG list <nanog@nanog.org>
Errors-To: nanog-bounces@nanog.org

OK... I'll bite...

The pedantic way:

No.  IP addresses are already integers.  All conversation
on this topic has been about how to convert between
different methods of representing integers, but, at the
end of the day, IP addresses are either 32 (IPv4) or
128 (IPv6) bit integers. There is no conversion possible
or required between IP addresses and integers as the
set of {IP Addresses} is a true subset of the set {integers}.

Owen

On Aug 27, 2008, at 10:07 AM, Robert Kisteleki wrote:

> Colin Alston wrote:
>> On 2008/08/27 05:22 PM Dave Israel wrote:
>>>
>>> Normally, I don't participate in this sort of thing, but I'm a  
>>> sucker for a "there's more than one way to do it" challenge.
>> Aww come on, C gets way more "fun" than that ;)
>> #define _u8 unsigned char
>> #define _u32 unsigned long
>> int main(void) {
>>    _u32 ipn = 1089055123;
>>    _u8 ipa[3];
>>    _u8 oct = 0;
>>    for (oct=0; oct <4; oct++){
>>        ipa[oct] = (char)(
>>            (ipn & (0xFF000000 >> (8*oct))) >> (8*(3-oct))
>>        );
>>    }
>>    printf("%d.%d.%d.%d\n", ipa[0], ipa[1], ipa[2], ipa[3]);
>>    return 0;
>> }
>
> Actually, who needs loops for that?
>
> #include <stdio.h>
>
> int main()
> {
> 	unsigned i = 1089055123;
> 	printf("%d.%d.%d.%d\n",
> (unsigned char)(((char*)&i)[3]),
> (unsigned char)(((char*)&i)[2]),
> (unsigned char)(((char*)&i)[1]),
> (unsigned char)(((char*)&i)[0])
> );
> 	return 0;
> }
>
>
> Robert



home help back first fref pref prev next nref lref last post