douxi2011 2019-06-03 19:30
浏览 230

如何使用Golang移位十六进制值?

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?

  • 写回答

1条回答 默认 最新

  • douliao7354 2019-06-04 04:35
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表