You can try these functions:

proc inet_addr { ipaddr } {

    set addr [binary format c4 [split $ipaddr .]]
    binary scan $addr i bin
    return $bin
}

proc inet_ntoa { ipaddr } {

    set bin [binary format i $ipaddr]
    binary scan $bin cccc a b c d
    set a [expr { ($a + 0x100) % 0x100 }]
    set b [expr { ($b + 0x100) % 0x100 }]
    set c [expr { ($c + 0x100) % 0x100 }]
    set d [expr { ($d + 0x100) % 0x100 }]
    return "$a.$b.$c.$d"
}

On Mon, Feb 25, 2002 at 02:09:51PM -0500, [EMAIL PROTECTED] wrote:
> I am working on a back-end logging system and am having a bit of difficulty
> with a number conversion. The resource that provides the IP address to log
> can only give it in Long format. I have come up with a function to convert
> the Long IP into Dotted Quad format, but the first division returns a
> negative value whenever the Long IP provided is greater than 2147483647.  Can
> anyone help figure out why expr will not accept any values bigger than that,
> and what my options are? I have looked through the AOLServer functions to see
> if there was anything built-in that would work but nothing caught my eye.
>
> Thanks!
>
>
>
> proc dotted_ip_from_long { long_ip } {
>
> set n 1
> set ip_parts [list]
>
> while { $n < 5 } {
>
>      set denom [expr int(pow(256,(4 - $n)))]
>      set ip_part [expr int(floor($long_ip / $denom ))]
>      set long_ip [expr $long_ip % $denom ]
>      lappend ip_parts $ip_part
>      incr n
>      }
>
> return [join $ip_parts "."]
>
> }

Reply via email to