Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: Decimal to IP address

From: holger:rebol at: 11-Dec-2000 14:17

On Mon, Dec 11, 2000 at 11:59:40AM -0800, Kevin McKinnon wrote:
> On Sat, 9 Dec 2000, Paul E Tretter wrote: > > > > > Anyone have an easy solution for converting a 32 bit decimal number to ip address. > > > > Paul Tretter > > Hi Paul, > > How about: > > dectoip: make function! [netdec] [ > d: netdec // 256 > c: (to-integer netdec / 256) // 256 > b: (to-integer netdec / 65536) // 256 > a: (to-integer netdec / 16777216) > ip: to-tuple rejoin [a "." b "." c "." d] > ] > > print dectoip 3509897244 > == 209.52.200.28
A problem is that you would need to know whether the integer is in host byte order or network byte order. For network byte order the above function looks correct. For host byte order it would depend on whether the number was created on a big endian or little endian machine. For big endian the function is still correct, but for little endian you need to use ip: to-tuple rejoin [d "." c "." b "." a] to form the IP address. -- Holger Kruse [holger--rebol--com]