作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
golang有没有类似PHP函数的包bcsub , bcadd等等?
我正在尝试将以下函数从 php 编写到 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";
}
最佳答案
您可以使用 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()
}
关于php - Golang - 有 bcadd/bcsub 包吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36368343/
golang有没有类似PHP函数的包bcsub , bcadd等等? 我正在尝试将以下函数从 php 编写到 golang。 function convertToSteamID($communityI
我是一名优秀的程序员,十分优秀!