How to do conversion from IP integer to string in golang

10,479 views
Skip to first unread message

MichaelXu

unread,
May 8, 2012, 6:01:54 AM5/8/12
to golan...@googlegroups.com
Does golang have functions like inet_ntoa?

Jan Mercl

unread,
May 8, 2012, 9:22:24 AM5/8/12
to MichaelXu, golan...@googlegroups.com
On Tue, May 8, 2012 at 12:01 PM, MichaelXu <starsta...@gmail.com> wrote:
Does golang have functions like inet_ntoa?

fmt.Sprintf("%d.%d.%d.%d", byte(a)>>24, byte(a)>>16, byte(a)>>8, byte(a))

or



andrey mirtchovski

unread,
May 8, 2012, 9:26:50 AM5/8/12
to Jan Mercl, MichaelXu, golan...@googlegroups.com

MichaelXu

unread,
May 8, 2012, 10:42:57 PM5/8/12
to golan...@googlegroups.com
Thanks!

A little fix, this code snippet works:

fmt.Sprintf("%d.%d.%d.%d", byte(a>>24), byte(a>>16), byte(a>>8), byte(a))

Jan Mercl

unread,
May 9, 2012, 2:47:12 AM5/9/12
to MichaelXu, golan...@googlegroups.com
On Wed, May 9, 2012 at 4:42 AM, MichaelXu <starsta...@gmail.com> wrote:
A little fix, this code snippet works:

fmt.Sprintf("%d.%d.%d.%d", byte(a>>24), byte(a>>16), byte(a>>8), byte(a))

Oops, sorry for the mistake, you're right ;-)

Russ Cox

unread,
May 9, 2012, 11:19:48 AM5/9/12
to MichaelXu, golan...@googlegroups.com
On Tue, May 8, 2012 at 10:42 PM, MichaelXu <starsta...@gmail.com> wrote:
> A little fix, this code snippet works:
>
> fmt.Sprintf("%d.%d.%d.%d", byte(a>>24), byte(a>>16), byte(a>>8), byte(a))

All the more reason to use the IP type.

Russ

pa...@vanbrouwershaven.com

unread,
May 10, 2012, 6:57:26 AM5/10/12
to golan...@googlegroups.com, MichaelXu, r...@golang.org
I think it make sense to use the IP type for this conversion, but how do you convert an int value into an IP type and back?

Kyle Lemons

unread,
May 10, 2012, 2:43:47 PM5/10/12
to pa...@vanbrouwershaven.com, golan...@googlegroups.com, MichaelXu, r...@golang.org
On Thu, May 10, 2012 at 3:57 AM, <pa...@vanbrouwershaven.com> wrote:
I think it make sense to use the IP type for this conversion, but how do you convert an int value into an IP type and back?

Where are you given an integer that is actually an IP address?  I would go after that, since it's not really an appropriate representation (uint32 can work, in which case you do the appropriate bit shifting and masking to translate back and forth, but this shouldn't be common).

Paul van Brouwershaven

unread,
May 10, 2012, 2:53:37 PM5/10/12
to Kyle Lemons, golan...@googlegroups.com, MichaelXu, r...@golang.org
I created a solution that works for me. Two functions that do the conversion between net.IP and int64 (I'm using int64 as mongodb does not support uint). The reason that I'm using an integer is that it can easily be used to link a certain ip address with an iprange.

But of course It would be better to have a standard solution in the net package that does also work for IPv6.

// Convert uint to net.IP
func inet_ntoa(ipnr int64) net.IP {   
    var bytes [4]byte
    bytes[0] = byte(ipnr & 0xFF)
    bytes[1] = byte((ipnr >> 8) & 0xFF)
    bytes[2] = byte((ipnr >> 16) & 0xFF)
    bytes[3] = byte((ipnr >> 24) & 0xFF)

    return net.IPv4(bytes[3],bytes[2],bytes[1],bytes[0])
}

// Convert net.IP to int64
func inet_aton(ipnr net.IP) int64 {      
    bits := strings.Split(ipnr.String(), ".")
    
    b0, _ := strconv.Atoi(bits[0])
    b1, _ := strconv.Atoi(bits[1])
    b2, _ := strconv.Atoi(bits[2])
    b3, _ := strconv.Atoi(bits[3])

    var sum int64
    
    sum += int64(b0) << 24
    sum += int64(b1) << 16
    sum += int64(b2) << 8
    sum += int64(b3)
    
    return sum

Paul van Brouwershaven

unread,
May 10, 2012, 3:02:54 PM5/10/12
to Kyle Lemons, golan...@googlegroups.com, MichaelXu, r...@golang.org
One other important reason I forgot to mention is that you can do some calculations to define range sizes and simply +1 to get the next or -1 to get the previous IP number in the range. The ranges can start at any place and don't have to be a CIDR block.
--

--
Regards,

Paul van Brouwershaven


楚杰

unread,
May 10, 2012, 9:11:34 PM5/10/12
to golan...@googlegroups.com, golan...@googlegroups.com
I think you can use encoding/binary package to convert between uint32 and []byte (hence net.IP) types.
Reply all
Reply to author
Forward
0 new messages