Go 在运行时将2^8位表示转换为BigInteger
我这里有下面的代码片段,我想转换为GoGo 在运行时将2^8位表示转换为BigInteger,go,math,Go,Math,我这里有下面的代码片段,我想转换为Go BigInteger b = BigInteger.ZERO; for (int i = 0; i < bytes.length; i++) { b = b.add(BigInteger.ONE.multiply(BigInteger.valueOf(bytes[i] & 0xff)).shiftLeft(i * 8)); } return b; 但结果似乎仍然不一致。我遗漏了什么吗?尝试将代
BigInteger b = BigInteger.ZERO;
for (int i = 0; i < bytes.length; i++) {
b = b.add(BigInteger.ONE.multiply(BigInteger.valueOf(bytes[i] & 0xff)).shiftLeft(i * 8));
}
return b;
但结果似乎仍然不一致。我遗漏了什么吗?尝试将代码从一种语言转换为另一种语言很少是个好主意。用Go编写函数。比如说,
package main
import (
"fmt"
"math"
"math/big"
)
func BytesToBigInteger(b []byte) *big.Int {
return new(big.Int).SetBytes(b)
}
func main() {
b := []byte{1, 1}
i := BytesToBigInteger(b)
fmt.Println(i, uint16(1<<8+1))
b = []byte{255, 255, 255, 255}
i = BytesToBigInteger(b)
fmt.Println(i, uint32(math.MaxUint32))
}
尝试将代码从一种语言转换为另一种语言很少是个好主意。用Go编写函数。比如说,
package main
import (
"fmt"
"math"
"math/big"
)
func BytesToBigInteger(b []byte) *big.Int {
return new(big.Int).SetBytes(b)
}
func main() {
b := []byte{1, 1}
i := BytesToBigInteger(b)
fmt.Println(i, uint16(1<<8+1))
b = []byte{255, 255, 255, 255}
i = BytesToBigInteger(b)
fmt.Println(i, uint32(math.MaxUint32))
}
为什么不直接使用
biginger.valueOf(1)为什么不直接使用biginger.valueOf(1
257 257
4294967295 4294967295