I am trying to convert EUI64 to EUI48 using goLang, but I don't seem to find any straight forward solution. Could anyone please help me with this.
You have to clear bytes in the middle (0xfffe) and toggle the 7th most significant bit:
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
}
input:
0221.86ff.feb5.6e10
output:
02186fffef56