-3

How can I convert the time since epoch to Hex in Go?

It should look like this: 5E839BAB

See: https://www.epochconverter.com/hex

EDIT: I was not able to find anything similar asked already. My Plan was to get the current time in Unix (Epoch) -> convert it to a Byte Array and then use hex.EncodeToString() to get it as hex

JimB
  • 104,193
  • 13
  • 262
  • 255
  • 1
    It's better to include the relevant information to the original question. If the obvious answer isn't what you want, add that. [ask] – JimB Mar 31 '20 at 20:29

1 Answers1

4

You can get the epoch of time value using t.Unix():

t:=time.Now()
fmt.Sprintf("%X",t.Unix())

To get this as a byte array:

import "encoding/binary"
...
out:=make([]byte,4)
binary.LittleEndian.PutUint32(out,uint32(t.Unix()))

Or, use BigEndian.

Burak Serdar
  • 46,455
  • 3
  • 40
  • 59