interger to I P address

Dave Israel davei at otd.com
Wed Aug 27 10:22:14 CDT 2008


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.

Shadow wrote:
> Robert D. Scott wrote:
>   
>> The harder way:
>>
>> Decimal: 1089055123
>> Hex (dashes inserted at octals): 40-E9-A9-93
>> Decimal (of each octet): 64-233-169-147
>> IP Address: 64.233.169.147
>>     
>
>
> The "this could take all day" way :
>
> (in bc with scale=0 for integer portions only)
>
> 1089055123/(2^24)%(2^8)
> 64
> 1089055123/(2^16)%(2^8)
> 233
> 1089055123/(2^8)%(2^8)
> 169
> 1089055123/(2^0)%(2^8)
> 147
>
> (Note: 2^0=1 & x/1=x so last line could reduce to 1089055123%(2^8).)
>
> -Nicholas
> shadow at gti.net
>
>   

The "ugly, please adjust according to your endianness, etc" way:

int *dec;
unsigned char *oct1, *oct2, *oct3, *oct4;

main(int argc, char **argv) {
  dec = malloc(sizeof(int));
  *dec = 1089055123;
  oct4 = dec;
  oct3 = oct4 + sizeof(char);
  oct2 = oct3 + sizeof(char);
  oct1 = oct2 + sizeof(char);

  printf("dec: %lu  ip: %hu.%hu.%hu.%hu\n", *dec, *oct1, *oct2, *oct3, 
*oct4);
}





More information about the NANOG mailing list