1

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.

Community
  • 1
  • 1
James Sapam
  • 16,036
  • 12
  • 50
  • 73

1 Answers1

2

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.

Community
  • 1
  • 1
Andy Schweig
  • 6,597
  • 2
  • 16
  • 22
  • some eg or snippet would be very helpful, i already read and tried various options about converting that. – James Sapam Nov 27 '16 at 20:55
  • 1
    Sorry, was answering from my phone and thought that might get you started. I'll put in an example when next in front of a computer (if someone else doesn't do it first). – Andy Schweig Nov 28 '16 at 00:57
  • Before I add more information to my answer, do you want to create a Go `Time` value out of that information or do you just need to print the date and time in the format you showed? Also, can you tell me what aspect of this is giving you difficulty? – Andy Schweig Nov 28 '16 at 03:47
  • the problem I am facing right now is while converting to string from the byte array, when i tried i got something like this `�*` which is not readable. https://play.golang.org/p/CXhjkazUvA – James Sapam Nov 28 '16 at 03:48
  • The byte slice contains the octets as described in the SNMP data and time format (link in my answer). Each byte (or two bytes in the case of the year) contains one component of the date and time. The sequence of bytes itself is not a string – it's a set of numeric values as described in the format specification. It looks like your `Convert2String` function is looking for a 0 byte as an indication of the end of the string, but that's not appropriate here. See https://play.golang.org/p/fICpTdk-2r. – Andy Schweig Nov 28 '16 at 04:17
  • Awesome, could you please update in your answer, so i can accept it. thank you so much. – James Sapam Nov 28 '16 at 04:20