convert hex ip addres to net.IP

2,471 views
Skip to first unread message

Vasiliy Tolstov

unread,
Feb 19, 2013, 5:21:53 AM2/19/13
to golan...@googlegroups.com
Hello gophers !

I can get needed if address from /proc/net/route (i'm tries to find
default gateway) but address writing in hexedecimal. How can i convert
hex to net.IP ?


--
Vasiliy Tolstov,
Clodo.ru
e-mail: v.to...@selfip.ru
jabber: va...@selfip.ru

minux

unread,
Feb 19, 2013, 5:26:42 AM2/19/13
to Vasiliy Tolstov, golan...@googlegroups.com
On Tue, Feb 19, 2013 at 6:21 PM, Vasiliy Tolstov <v.to...@selfip.ru> wrote:
I can get needed if address from /proc/net/route (i'm tries to find
default gateway) but address writing in hexedecimal. How can i convert
hex to net.IP ?
use encoding/hex to unhexify the bytes, and rearrange it according to the
endianness of host machine and then use net.IPv4 to construct a net.IP.

Vasiliy Tolstov

unread,
Feb 19, 2013, 6:27:01 AM2/19/13
to minux, golan...@googlegroups.com
But if the ip not ipv4 how can i construct ip address?

2013/2/19 minux <minu...@gmail.com>:

minux

unread,
Feb 19, 2013, 6:59:58 AM2/19/13
to Vasiliy Tolstov, golan...@googlegroups.com
On Tue, Feb 19, 2013 at 7:27 PM, Vasiliy Tolstov <v.to...@selfip.ru> wrote:
But if the ip not ipv4 how can i construct ip address?
after unhexify the address to a byte slice and rearrange the byte order, just cast it to net.IP
(make sure the length is 4 or 16 for IPv4 and IPv6, respectively)

Vasiliy Tolstov

unread,
Feb 19, 2013, 7:41:43 AM2/19/13
to minux, golan...@googlegroups.com
2013/2/19 minux <minu...@gmail.com>:

> after unhexify the address to a byte slice and rearrange the byte order,
> just cast it to net.IP
> (make sure the length is 4 or 16 for IPv4 and IPv6, respectively)

Thanks =)! Last question - how can i rearrange byte order ? This is
nothing outputs on last Printf.

func main() {
var ip []uint8
b, err := hex.DecodeString("0101A8C0")
if err != nil {
return
}
fmt.Printf("%+v\n", b)
buf := bytes.NewBuffer(b)
err = binary.Read(buf, binary.LittleEndian, &ip)
if err != nil {
fmt.Println("binary.Read failed:", err)
}
fmt.Printf("%+v\n", ip)
}


--
Vasiliy Tolstov,
Clodo.ru
e-mail: v.to...@selfip.ru
jabber: va...@selfip.ru

2013/2/19 minux <minu...@gmail.com>:

Kyle Lemons

unread,
Feb 19, 2013, 4:42:09 PM2/19/13
to Vasiliy Tolstov, minux, golang-nuts
Have you considered using http://golang.org/pkg/net/#Interface ?


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Vasiliy Tolstov

unread,
Feb 20, 2013, 7:47:50 AM2/20/13
to Kyle Lemons, minux, golang-nuts
Thanks, Kyle, but how can i get gateway info?
After some reading i found 3 solutions - exec external app (not good),
read /proc/net/route (choised), use netlink (in case of golang some
netlink libraries present...)

2013/2/20 Kyle Lemons <kev...@google.com>:

Kyle Lemons

unread,
Feb 20, 2013, 6:03:11 PM2/20/13
to Vasiliy Tolstov, minux, golang-nuts
oh, for some reason I was thinking you wanted an interface IP.  The default gateway can be ascertained by using the `route` tool, which will give you IP addreses in dotted decimal form.  You can get this using syscalls, but route is probably more portable.

Vasiliy Tolstov

unread,
Feb 20, 2013, 11:38:34 PM2/20/13
to Kyle Lemons, minux, golang-nuts
route tool may be absent..
Okay if i'm reverse bytes after hex.DecodeString and construct ip
address does it right?

func main() {
var ip []uint8
b, err := hex.DecodeString("0101A8C0")
if err != nil {
return
}
fmt.Printf("%+v\n", b)
}

2013/2/21 Kyle Lemons <kev...@google.com>:

Rich

unread,
Feb 21, 2013, 11:25:38 AM2/21/13
to golan...@googlegroups.com, Kyle Lemons, minux
Not sure if this answers the question or not, but here is a way to convert hex ip to decimal ip:

package main

import "fmt"
import "encoding/hex"


func main() {
    z:="0A010100"
    a,_:=hex.DecodeString(z)
    fmt.Printf("%v.%v.%v.%v",a[0],a[1],a[2],a[3])

Vasiliy Tolstov

unread,
Feb 22, 2013, 1:43:47 AM2/22/13
to Rich, golan...@googlegroups.com, Kyle Lemons, minux
2013/2/21 Rich <rma...@gmail.com>:
> Not sure if this answers the question or not, but here is a way to convert
> hex ip to decimal ip:
>
> package main
>
> import "fmt"
> import "encoding/hex"
>
>
> func main() {
> z:="0A010100"
> a,_:=hex.DecodeString(z)
> fmt.Printf("%v.%v.%v.%v",a[0],a[1],a[2],a[3])
>
> }


Thanks, i'm already do that =)
Reply all
Reply to author
Forward
0 new messages