Last active
August 29, 2015 13:56
-
-
Save kalimatas/8999511 to your computer and use it in GitHub Desktop.
Convert 32 bitwise IP representation to string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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