golang[44]-blockchain-targetHash

栏目: Go · 发布时间: 7年前

内容简介:以之前的bits:181B7B74为例子

比特币目标hash计算过程

以之前的bits:181B7B74为例子

func main(){

	bits,_:= hex.DecodeString("181B7B74")


	fmt.Printf("%x",CalculateTargetFast(bits))

}
//18   1B7B74
func CalculateTargetFast(bits []byte) []byte{

		var result []byte
		//第一个字节  计算指数
		exponent := bits[:1]
		fmt.Printf("%x\n",exponent)

		//计算后面3个字节 系数
		coeffient:= bits[1:]
	fmt.Printf("%x\n",coeffient)


		//将字节,他的16进制为"18"  转化为了string "18"
		str:= hex.EncodeToString(exponent)  //"18"
		fmt.Printf("str=%s\n",str)
	   //将字符串18转化为了10进制int64 24
		exp,_:=strconv.ParseInt(str,16,8)

			fmt.Printf("exp=%d\n",exp)
		//拼接,计算出目标hash
		result  = append(bytes.Repeat([]byte{0x00},32-int(exp)),coeffient...)
		result  =  append(result,bytes.Repeat([]byte{0x00},32-len(result))...)


	return result
}

以上所述就是小编给大家介绍的《golang[44]-blockchain-targetHash》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Software Paradigms

Software Paradigms

Stephen H. Kaisler / Wiley-Interscience / 2005-03-17 / USD 93.95

Software Paradigms provides the first complete compilation of software paradigms commonly used to develop large software applications, with coverage ranging from discrete problems to full-scale applic......一起来看看 《Software Paradigms》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

MD5 加密
MD5 加密

MD5 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具