douyin4561 2016-11-27 18:36
浏览 281
已采纳

在GoLang中将日期时间OctetString转换为人类可读的字符串

How to convert Datetime Octetstring to ASCII. I read through one of the example in python netsnmp but still not able to solve it.

This is what I received from gosnmp as slice of []uint8

[7 224 1 28 20 5 42 0 43 0 0]

or Go-syntax representation of the value

[]byte{0x7, 0xe0, 0x1, 0x1c, 0x14, 0x4, 0x2a, 0x0, 0x2b, 0x0, 0x0}

And output in datetime should be something like this:

2015-10-7,17:23:27.0,+0:0

here is the mibs:oid: HOST-RESOURCES-MIB::hrSWInstalledDate

Can someone give me some idea on how to use binary to decode that into human readable ascii or strings.

  • 写回答

1条回答 默认 最新

  • duanquezhan7268 2016-11-27 19:39
    关注

    The format is described here: Convert snmp octet string to human readable date format

    If you just need to create a date/time string in the format you mentioned, this will do it:

    func snmpTimeString(c []byte) string {
        year := (int(c[0]) << 8) | int(c[1])
        return fmt.Sprintf("%d-%d-%d,%02d:%02d:%02d.%d,%c%d:%d", year, c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[10])
    }
    
    func main() {
        c := []byte{0x7, 0xe0, 0x1, 0x1c, 0x14, 0x4, 0x2a, 0x0, 0x2b, 0x0, 0x0}
        fmt.Println(snmpTimeString(c))
    }
    

    See https://play.golang.org/p/7WwQbPuESC.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了