-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
  • 96,249
  • 11
  • 237
  • 229
  • 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

2

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
  • 37,605
  • 3
  • 27
  • 46