题解 | #IP地址转化#
IP地址转化
https://www.nowcoder.com/practice/67ae7c321ae147049ad569c8fe5c98cd?tpId=196&tqId=40406&ru=/exam/oj
package main import ( "strconv" "strings" ) /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param ip string字符串 * @return string字符串 */ func IPtoNum(ip string) string { words := strings.Split(ip, ".") res := []string{} for i := 0; i < len(words); i++ { v, _ := strconv.Atoi(words[i]) ans := strconv.FormatInt(int64(v), 2) for len(ans) < 8 { ans = "0" + ans } res = append(res, ans) } t := strings.Join(res, "") r1, _ := strconv.ParseUint(t, 2, 64) return strconv.FormatUint(r1, 10) }