使用多种方式转换十六进制值会导致不同的输出
使用两个不同的 func 转换十六进制值会导致输出略有不同
例如
b, _ := new(big.Int).SetString("00662ad25db00e7bb38bc04831ae48b4b446d12698", 16)
fmt.Println(b.Bytes())
// output [102 42 210 93 176 14 123 179 139 192 72 49 174 72 180 180 70 209 38 152]
fmt.Println(hex.DecodeString("00662ad25db00e7bb38bc04831ae48b4b446d12698"))
// output [0 102 42 210 93 176 14 123 179 139 192 72 49 174 72 180 180 70 209 38 152]
在操场上运行它。
为什么 DecodeString 有一个前导 0 而 big.Int 没有?
回答
该Int.SetString方法将字符串解释为编码数字。前导零在数字编码中并不重要。例如,十六进制数字编码 0001 和 1 都表示数字 1。
该hex.DecodeString函数将字符串解释为十六进制编码的字节。前导零字节很重要。