This commit is contained in:
2025-02-06 11:14:33 +08:00
commit 07847a2d9e
535 changed files with 65131 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package biginthelper
import (
"math/big"
"regexp"
"strconv"
)
// ParseBigInt 将字符串转换为 big.Int
func ParseBigInt(value string) *big.Int {
bi := new(big.Int)
i, ok := bi.SetString(value, 10)
if !ok {
return bi
}
return i
}
// IntToHex convert int to hexadecimal representation
// int64转换为16进制字符串
func IntToHex(i int64) string {
return strconv.FormatInt(i, 16)
}
// BigToHex 将bigint转化为16进制带 0x 的字符串
func BigToHex(bigInt big.Int) string {
return "0x" + IntToHex(bigInt.Int64())
}
// CheckIsAddress Check is a eth Address
// 检查是否是以太坊地址 正则匹配
func CheckIsAddress(addr string) bool {
re := regexp.MustCompile("^0x[0-9a-fA-F]{40}$")
return re.MatchString(addr)
}

View File

@ -0,0 +1,55 @@
package biginthelper
import (
"fmt"
"math/rand"
"strconv"
"strings"
"testing"
)
func Test_ParseBigInt(t *testing.T) {
fmt.Println(ParseBigInt(`231513`))
fmt.Println(ParseBigInt(`654wdf16`))
fmt.Println(ParseBigInt(`5455_1655`))
fmt.Println(ParseBigInt(``))
fmt.Println(ParseBigInt(`af`))
}
func Test_IntToHex(t *testing.T) {
fmt.Println(strings.TrimLeft(`01615`, "0"))
fmt.Println(strings.TrimLeft(`1615`, "0"))
fmt.Println(strings.TrimLeft(`0x1615`, "0"))
}
// i := int64(32)
// s := strconv.FormatInt(i, 16)
// println(s)
// 对比strconv.FormatInt(i, 16)和fmt.Sprintf("0x%x", i)的性能消耗
// go test -bench=_QE_ -benchmem
// -benchtime 默认为1秒 -benchmem 获得内存分配的统计数据
func Benchmark_QE_strconv(b *testing.B) {
for i := 0; i < b.N; i++ {
strconv.FormatInt(getInt64(), 16)
}
}
func Benchmark_QE_fmt(b *testing.B) {
for i := 0; i < b.N; i++ {
fmt.Sprintf("0x%x", getInt64())
}
}
func getInt64() int64 {
return int64(rand.Intn(10000))
}
// 结果
// Benchmark_QE_strconv-6 47142570 24.29 ns/op 5 B/op 1 allocs/op
// Benchmark_QE_fmt-6 14787649 82.41 ns/op 8 B/op 1 allocs/op
// 改为随机数后
// Benchmark_QE_strconv-6 27890760 42.48 ns/op 3 B/op 0 allocs/op
// Benchmark_QE_fmt-6 10595380 108.6 ns/op 15 B/op 1 allocs/op
// 结论 尽量使用 strconv.FormatInt(i, 16) 进行16进制的转换

View File

@ -0,0 +1,119 @@
package biginthelper
import (
"bytes"
"fmt"
"math/big"
"strings"
)
// BigIntString BigIntString
func BigIntString(balance *big.Int, decimals int64) string {
amount := BigIntFloat(balance, decimals)
deci := fmt.Sprintf("%%0.%vf", decimals)
return clean(fmt.Sprintf(deci, amount))
}
// BigIntFloat BigIntFloat
func BigIntFloat(balance *big.Int, decimals int64) *big.Float {
if balance.Sign() == 0 {
return big.NewFloat(0)
}
bal := new(big.Float)
bal.SetInt(balance)
pow := bigPow(10, decimals)
p := big.NewFloat(0)
p.SetInt(pow)
bal.Quo(bal, p)
return bal
}
func bigPow(a, b int64) *big.Int {
r := big.NewInt(a)
return r.Exp(r, big.NewInt(b), nil)
}
func clean(newNum string) string {
stringBytes := bytes.TrimRight([]byte(newNum), "0")
newNum = string(stringBytes)
if stringBytes[len(stringBytes)-1] == 46 {
newNum += "0"
}
if stringBytes[0] == 46 {
newNum = "0" + newNum
}
return newNum
}
// GetActualHex 获取真实的十六进制..
func GetActualHex(h string) string {
h = strings.TrimLeft(h, "0")
var hex string
if strings.Index(h, "0x") == 0 {
hex = h[2:]
} else {
hex = h
}
if len(h)%2 != 0 {
hex = "0" + hex
}
return "0x" + hex
}
// HexToBig HexToBig
func HexToBig(h string) *big.Int {
i := big.NewInt(0)
h = strings.Replace(h, "0x", "", -1)
if h == "" {
return i
}
if _, ok := i.SetString(h, 16); !ok {
return nil
}
return i
}
// ConvertNumToFloat ConvertNumToFloat
func ConvertNumToFloat(num int64) float64 {
switch num {
case 1:
return 10.0
case 2:
return 100.0
case 3:
return 1000.0
case 4:
return 10000.0
case 5:
return 100000.0
case 6:
return 1000000.0
case 7:
return 10000000.0
case 8:
return 100000000.0
case 9:
return 1000000000.0
case 10:
return 10000000000.0
case 11:
return 100000000000.0
case 12:
return 1000000000000.0
case 13:
return 10000000000000.0
case 14:
return 100000000000000.0
case 15:
return 1000000000000000.0
case 16:
return 10000000000000000.0
case 17:
return 100000000000000000.0
case 18:
return 1000000000000000000.0
default:
return 0.0
}
}