feat: 转换IP位网络地址掩码

This commit is contained in:
TsMask
2024-05-09 15:39:16 +08:00
parent 077a3cfc64
commit 3d3f3e9634

View File

@@ -170,6 +170,27 @@ func Color(colorStr string) *color.RGBA {
}
}
// ConvertIPMask 转换IP网络地址掩码 24 -> 255.255.255.0
func ConvertIPMask(bits int64) string {
if bits < 0 || bits > 32 {
return "Invalid Mask Bits"
}
// 构建一个32位的uint32类型掩码指定前bits位为1其余为0
mask := uint32((1<<bits - 1) << (32 - bits))
// 将掩码转换为四个八位分组
groups := []string{
fmt.Sprintf("%d", mask>>24),
fmt.Sprintf("%d", (mask>>16)&255),
fmt.Sprintf("%d", (mask>>8)&255),
fmt.Sprintf("%d", mask&255),
}
// 将分组用点号连接起来形成掩码字符串
return strings.Join(groups, ".")
}
// ConvertConfigToMap 将配置内容转换为Map结构数据
//
// configType 类型支持txt json yaml yml