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

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";
}

图片转代码服务由CSDN问答提供 功能建议

golang中是否有类似于PHP函数的包 bcsub bcadd etc.<

nn

我正在尝试将以下函数从php编写为golang。

  function convertToSteamID(  $ communityID){
 //看看steamid中的第二个数字(auth服务器)是0还是1.奇数是1,偶数是0 
 $ authserver = bcsub($ communityID,'76561197960265728')&amp;  1; 
 //获取第三个数字的steamid 
 $ authid =(bcsub($ communityID,'76561197960265728') -  $ authserver)/ 2; 
 //连接STEAM_前缀和第一个数字,即 总是0,以及其他两个数字的冒号
返回“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()
    }
    

    playground

    • edit: added the reverse function as well
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题