将 EUI 64 转换为 EUI 48 或 Mac 地址
Convert EUI64 to EUI48 or MacAddress
我正在尝试使用 goLang 将 EUI64 转换为 EUI48,但我似乎没有找到任何直接的解决方案。谁能帮我解决这个问题。
您必须清除中间的字节 (0xfffe) 并切换第 7 个最高有效位:
package main
import (
"fmt"
"strconv"
"regexp"
)
func main() {
var eui64 string = "0221.86ff.feb5.6e10"
fmt.Printf("%s\n", eui64)
var eui48 string = from_eui_64_to_48(eui64)
fmt.Printf("%s\n",eui48)
}
func from_eui_64_to_48(s string) string {
reg, err := regexp.Compile("[^a-fA-F0-9]+") // keep only hex characters
if err != nil {
panic(err)
}
hexString := reg.ReplaceAllString(s, "")
n, err := strconv.ParseInt(hexString, 16, 64) // convert string to int64
if err != nil {
panic(err)
}
n ^= 0x0200000000000000 // toggle the 7th most significant bit of 64 bits integer
n = (n >> 12) | (n & 0xffffff) // keep 3 bytes from the left and 3 bytes from the right, clear 0xfffe
return fmt.Sprintf("%012x",n) // return string
}
输入:
0221.86ff.feb5.6e10
输出:
02186fffef56
我正在尝试使用 goLang 将 EUI64 转换为 EUI48,但我似乎没有找到任何直接的解决方案。谁能帮我解决这个问题。
您必须清除中间的字节 (0xfffe) 并切换第 7 个最高有效位:
package main
import (
"fmt"
"strconv"
"regexp"
)
func main() {
var eui64 string = "0221.86ff.feb5.6e10"
fmt.Printf("%s\n", eui64)
var eui48 string = from_eui_64_to_48(eui64)
fmt.Printf("%s\n",eui48)
}
func from_eui_64_to_48(s string) string {
reg, err := regexp.Compile("[^a-fA-F0-9]+") // keep only hex characters
if err != nil {
panic(err)
}
hexString := reg.ReplaceAllString(s, "")
n, err := strconv.ParseInt(hexString, 16, 64) // convert string to int64
if err != nil {
panic(err)
}
n ^= 0x0200000000000000 // toggle the 7th most significant bit of 64 bits integer
n = (n >> 12) | (n & 0xffffff) // keep 3 bytes from the left and 3 bytes from the right, clear 0xfffe
return fmt.Sprintf("%012x",n) // return string
}
输入:
0221.86ff.feb5.6e10
输出:
02186fffef56