On 2002.02.25, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> I have come up with a function to convert
> the Long IP into Dotted Quad format, [...]

I've been thinking about writing a "proc net" for a while that
exposes [net inet_ntoa $long] for a while, but ... I'm lazy.

proc inet_ntoa {long} {
    set hex [format %08x $long]
    for {set i 0} {$i < 8} {incr i 2} {
        lappend dec [format %u 0x[string range $hex $i [expr $i + 1]]]
    }
    return [join $dec .]
}

To test:

231.123.213.132 -> 3883652484

% inet_ntoa 3883652484
231.123.213.132

127.0.0.1 -> 2130706433

% inet_ntoa 2130706433
127.0.0.1

255.255.255.255 -> 4294967295

% inet_ntoa 4294967295
255.255.255.255


Performance isn't too bad under Tcl8, either:

% puts $tcl_patchLevel
8.3.3

% time {inet_ntoa 4294967295} 10000
771 microseconds per iteration

% expr 1 / (711 * pow(10, -6))
1406.4697609

Unless I'm reading that wrong, it's approximately able to execute
1400 per second.

Compare to your dotted_ip_from_long:

% dotted_ip_from_long 2130706433
127.0.0.1

% time {dotted_ip_from_long 2130706433} 10000
1265 microseconds per iteration

Just 200 usec short of twice as slow.

-- Dossy

--
Dossy Shiobara                       mail: [EMAIL PROTECTED]
Panoptic Computer Network             web: http://www.panoptic.com/
  "He realized the fastest way to change is to laugh at your own
    folly -- then you can let go and quickly move on." (p. 70)

Reply via email to