如何保留 big.Int.Bytes() len?
给定:
arr := make([]byte, 8)
fmt.Println(arr) // [0 0 0 0 0 0 0 0]
big := new(big.Int).SetBytes(arr).Bytes()
fmt.Println(big) // []
我尝试将数字 1 放入并得到这样的结果:
... // [0 0 0 0 0 0 0 1] from []byte
...// [1] from big.Int
我必须保留 8 的长度,但 big.Int 不允许这样做。怎样才能保存完好呢?
Given:
arr := make([]byte, 8)
fmt.Println(arr) // [0 0 0 0 0 0 0 0]
big := new(big.Int).SetBytes(arr).Bytes()
fmt.Println(big) // []
I tried to put number 1 in and got such results:
... // [0 0 0 0 0 0 0 1] from []byte
...// [1] from big.Int
I have to preserve the length of 8 but big.Int
doesn't allow it. How to be able to preserve it still?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
int
内部类型不保留其构造的字节,bytes
返回表示该值所需的最小字节数。为了将其变成固定长度的字节切片,请使用
fillbytes
方法。您需要确保该值适合提供的字节切片,否则会惊慌。示例:
输出:
The
Int
type internally does not preserve the bytes from which it was constructed, andBytes
returns the minimum number of bytes that are required to represent the value.In order to turn it into a byte slice of a fixed length, use the
FillBytes
method. You need to ensure that the value fits in the provided byte slice, otherwise it will panic.Example:
Output: