gpt4 book ai didi

go - 将自纪元以来的时间转换为十六进制或字节数组

转载 作者:行者123 更新时间:2023-12-01 22:43:21 24 4
gpt4 key购买 nike

如何将自纪元以来的时间转换为Go中的Hex?

它看起来应该像这样:5E839BAB

另请:https://www.epochconverter.com/hex

编辑:我无法找到已经问过的类似内容。我的计划是在Unix(Epoch)中获取当前时间->将其转换为字节数组,然后使用hex.EncodeToString()将其作为十六进制获取

最佳答案

您可以使用t.Unix()获得时间值的纪元:

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

将此作为字节数组获取:
import "encoding/binary"
...
out:=make([]byte,4)
binary.LittleEndian.PutUint32(out,uint32(t.Unix()))

或者,使用BigEndian。

关于go - 将自纪元以来的时间转换为十六进制或字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60958537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com