Skip to content

Instantly share code, notes, and snippets.

@kalimatas
Last active August 29, 2015 13:56
Show Gist options
  • Save kalimatas/8999511 to your computer and use it in GitHub Desktop.
Save kalimatas/8999511 to your computer and use it in GitHub Desktop.
Convert 32 bitwise IP representation to string
package main
import (
"fmt"
"math"
"strconv"
"strings"
)
func main() {
ip := "172.30.33.232"
fmt.Printf("ip: %s\n", ip)
ipDigitsRaw := strings.Split(ip, ".")
var ipDigits []uint64
for _, ch := range ipDigitsRaw {
dig, _ := strconv.ParseUint(ch, 10, 0)
ipDigits = append(ipDigits, dig)
}
ipInt := ipDigits[0]*uint64(math.Pow(256, 3)) +
ipDigits[1]*uint64(math.Pow(256, 2)) +
ipDigits[2]*uint64(math.Pow(256, 1)) +
ipDigits[3]*uint64(math.Pow(256, 0))
fmt.Printf("ip: %d\n", ipInt)
fmt.Printf("%d.%d.%d.%d", ipInt>>24, ipInt>>16&0xff, ipInt>>8&0xff, ipInt&0xff)
fmt.Println()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment