dtrgqjcd877528 2016-04-02 01:57
浏览 288
已采纳

Golang - 有一个bcadd / bcsub包吗?

Is there a package in golang similar to the PHP functions bcsub, bcadd etc.?

I am trying to write the following function from php to golang.

function convertToSteamID($communityID) {
    // See if the second number in the steamid (the auth server) is 0 or 1. Odd is 1, even is 0
    $authserver = bcsub($communityID, '76561197960265728') & 1;
    // Get the third number of the steamid
    $authid = (bcsub($communityID, '76561197960265728')-$authserver)/2;
    // Concatenate the STEAM_ prefix and the first number, which is always 0, as well as colons with the other two numbers
    return "STEAM_0:$authserver:$authid";
}
  • 写回答

1条回答 默认 最新

  • douzi5214 2016-04-02 02:42
    关注

    You can do it using big.Int:

    var (
        magic, _ = new(big.Int).SetString("76561197960265728", 10)
        one      = big.NewInt(1)
        two      = big.NewInt(2)
    )
    
    func commIDToSteamID(ids string) string {
        id, _ := new(big.Int).SetString(ids, 10)
        id = id.Sub(id, magic)
        isServer := new(big.Int).And(id, one)
        id = id.Sub(id, isServer)
        id = id.Div(id, two)
        return "STEAM_0:" + isServer.String() + ":" + id.String()
    }
    
    func steamIDToCommID(ids string) string {
        p := strings.Split(ids, ":")
        id, _ := new(big.Int).SetString(p[2], 10)
        id = id.Mul(id, two)
        id = id.Add(id, magic)
        auth, _ := new(big.Int).SetString(p[1], 10)
        return id.Add(id, auth).String()
    }
    

    <kbd>playground</kbd>

    • edit: added the reverse function as well
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题