英文:
convert the Mac address to HardwareAddr
问题
对于RuuviTags,我正在使用这个软件包来转换传感器数据。
我试图将MAC地址转换为HardwareAddr类型,因为我在配置中使用它来扫描ruuvi标签。但是我遇到了困难。无论是使用复制还是通过String,我都无法得到正确的值。我最终得到了类似这样的结果:
��M�<
由于MAC地址是一个[6]byte:如何将[6]byte转换为HardwareAddr?
示例:
var hwSensor net.HardwareAddr
hwSensor,_ = net.ParseMAC(sensor.MacAddress)
fmt.Println("hwSensor (Cfg):"+hwSensor.String())
...
deviceRaw ruuvitag.RAWv2
fmt.Println("hw (Sensor)(RAW):"+string(deviceRaw.MAC[:]))
//输出
//hwSensor (Cfg):e1:c7:4d:94:3c:2d
//hw (Sensor)(RAW):��M�<-
所以基本上我可以轻松地将一个字符串(来自配置)转换为HardwareAddr,并再次获取字符串(这里仅用于打印)。但是deviceRaw.MAC与hwSensor的比较失败,因为它们不匹配。
英文:
for the RuuviTags I'm using this package for converting the sensor data.
I try to convert the MAC address to the type HardwareAddr as I'm using this via config for the scanning of ruuvi tags. but I'm struggling. In each way (using copy or via String) I'm not getting the right values. I end up with something like this:
��M�<
As the MAC adress is a [6]byte: how can I convert a [6]byte to HardwareAddr ?
Example:
var hwSensor net.HardwareAddr
hwSensor,_ = net.ParseMAC(sensor.MacAddress)
fmt.Println("hwSensor (Cfg):"+hwSensor.String())
...
deviceRaw ruuvitag.RAWv2
fmt.Println("hw (Sensor)(RAW):"+string(deviceRaw.MAC[:]))
//output
//hwSensor (Cfg):e1:c7:4d:94:3c:2d
//hw (Sensor)(RAW):��M�<-
so basically I can easily convert a string (from the config) to HardwareAddr and get the string afterwards again (here: jsut for printing). But comparison of deviceRaw.MAC with hwSensor failes, as they do not match.
答案1
得分: 0
将一个 [6]byte
转换为 HardwareAddr
的方法是:将数组重新切片为 []byte
,然后使用普通的转换表达式获取 net.HardwareAddr
。
mac := [6]byte{0xCB, 0xB8, 0x33, 0x4C, 0x88, 0x4F}
addr := net.HardwareAddr(mac[:])
fmt.Println(addr)
// 输出: cb:b8:33:4c:88:4f
你可以在这里查看示例代码:https://play.golang.org/p/5MWGs8R93-M
英文:
"how can I convert a [6]byte to HardwareAddr?" -- Reslice the array to get a []byte
and then use a plain conversion expression to get the net.HardwareAddr
.
mac := [6]byte{0xCB, 0xB8, 0x33, 0x4C, 0x88, 0x4F}
addr := net.HardwareAddr(mac[:])
fmt.Println(addr)
// output: cb:b8:33:4c:88:4f
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论