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.

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

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀