1 Star 0 Fork 1

SillyMan / Go实用工具包

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
parser.go 693 Bytes
一键复制 编辑 原始数据 按行查看 历史
SillyMan 提交于 2021-12-31 09:22 . 完善测试用例
package macaddr
import (
"net"
"strings"
"gitee.com/sillyman/simpleUtil/common/misc"
)
// Parse 解析字符串为MAC
// 使用以下格式之一:
// 00:00:5e:00:53:01
// 00-00-5e-00-53-01
// 0000.5e00.5301
// 0000-5e00-5301
// 00005e005301
func Parse(s string) MAC {
sLen := len(s)
switch {
case sLen < 12:
return nil
case sLen == 12:
maddr := make(MAC, 0, 6)
for i := 0; i < 6; i++ {
n, ok := misc.Hex2Int(s[i*2 : (i*2)+2])
if !ok {
return nil
}
maddr = append(maddr, byte(n))
}
return maddr
default:
if s[4] == '-' {
s = strings.ReplaceAll(s, "-", ".")
}
hw, err := net.ParseMAC(s)
if err != nil {
return nil
}
return New(hw)
}
}
Go
1
https://gitee.com/sillyman/simpleUtil.git
git@gitee.com:sillyman/simpleUtil.git
sillyman
simpleUtil
Go实用工具包
5c98b36afa10

搜索帮助