Let's say I have a string with the value of 4ADDF6C259EBAFF8
.
Using this I want to get a timestamp, using the formula (hex(val) >> 25) + 1008000400
.
Using t he encoding/hex
package, I have come up with the following:
srcBytes := []byte(src)
dst := make([]byte, hex.EncodedLen(len(srcBytes)))
hex.Encode(dst, srcBytes)
After this, I need a way to bit shift dst
25 times, and then add a constant to it.
However dst
is of type []byte
.
I need it to be of type hex
so I can bit shift after. How do I convert []byte
so that it can be shifted?
Assuming your input string varies but has a maximum of 16 hex digits, you just convert to a 64 bit (unsigned) integer and do the math. I also prefixed your constant with 0x assuming it is hex (judging by the digits).
s := "4ADDF6C259EBAFF8"
if i, err := strconv.ParseUint(s, 16, 64); err == nil {
fmt.Printf("%x
", i >> 25 + 0x1008000400)
}
BTW hex is not a type but a way of displaying integers.