Skip to content

Instantly share code, notes, and snippets.

@flowck
Last active August 31, 2022 11:40
Show Gist options
  • Save flowck/602d1988cf2e3f198acc8985d3db0da6 to your computer and use it in GitHub Desktop.
Save flowck/602d1988cf2e3f198acc8985d3db0da6 to your computer and use it in GitHub Desktop.
import (
"fmt"
"regexp"
"strconv"
)
func ipToInt(value string) (int, error) {
r := regexp.MustCompile(`[a-z]|:|\.`) // match chars from a to z, or : (colon), or . (dot)
v := r.ReplaceAll([]byte(value), []byte("")) // replace all chars matched previously
return strconv.Atoi(string(v)) // convert to int
}
func main() {
ip1 := "2001:0db8:0:0:8d3:0:0:0"
ip2 := "35.192.0.0"
IpInt1, err := ipToInt(ip1)
if err != nil {
panic(err)
}
IpInt2, err := ipToInt(ip2)
if err != nil {
panic(err)
}
fmt.Println(IpInt1)
fmt.Println(IpInt2)
}
function ipToInt(value) {
return Number(value.replace(/[a-z]|:|\./g, ""))
}
const ip1 = "2001:0db8:0:0:8d3:0:0:0"
const ip2 = "35.192.0.0"
console.log("IPv6", ipToInt(ip1))
console.log("IPv4", ipToInt(ip2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment