Last active
August 31, 2022 11:40
-
-
Save firminochangani/602d1988cf2e3f198acc8985d3db0da6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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) | |
} |
This file contains hidden or 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
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