package strutilimport ()//// -------------------- convert base --------------------//const (Base10Chars = "0123456789"Base16Chars = "0123456789abcdef"Base32Chars = "0123456789abcdefghjkmnpqrstvwxyz"Base36Chars = "0123456789abcdefghijklmnopqrstuvwxyz"Base48Chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL"Base62Chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"Base64Chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/")// Base10Conv convert base10 string to new base string.func ( string, int) string { returnBaseConv(, 10, ) }// BaseConv convert base string by from and to base.//// Note: from and to base must be in [2, 64]//// Usage://// BaseConv("123", 10, 16) // Output: "7b"// BaseConv("7b", 16, 10) // Output: "123"func ( string, , int) string {if > 64 || < 2 { = 10 }if > 64 || < 2 { = 16 }returnBaseConvByTpl(, Base64Chars[:], Base64Chars[:])}// BaseConvInt convert base int to new base string.//// Usage://// BaseConv(123, 16) // Output: "7b"func ( uint64, int) string {if > 64 || < 2 { = 16 }// bigInt 支持 2-62 进制转换处理 TODOif <= 36 {returnstrconv.FormatUint(, ) }if <= 62 { := new(big.Int).SetUint64()return .Text() }returnBaseConvIntByTpl(, Base64Chars[:])}// BaseConvByTpl convert base string by template.//// Usage://// BaseConvert("123", Base62Chars, Base16Chars) // Output: "1e"// BaseConvert("1e", Base16Chars, Base62Chars) // Output: "123"func ( string, , string) string {if == {return }// convert to base 10varuint64if == Base10Chars {varerror , = strconv.ParseUint(, 10, 0)if != nil {basefn.Panicf("input is not a valid decimal number: %s(%v)", , ) } } else { := uint64(len())for , := range { = * + uint64(strings.IndexRune(, )) } }// convert to new basereturnBaseConvIntByTpl(, )}// BaseConvIntByTpl convert base int to new base string.func ( uint64, string) string {// convert to new basevarstring := uint64(len())for > 0 { = string([%]) + /= }return}
The pages are generated with Goldsv0.8.4. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.