From dee606e6ab14096da1c1f079dad367cfeb33898f Mon Sep 17 00:00:00 2001 From: sre Date: Wed, 22 Jun 2022 18:47:56 +0800 Subject: [PATCH] =?UTF-8?q?62=E8=BF=9B=E5=88=B6=E6=95=B0=E5=AD=97=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- str/bit62.go | 41 +++++++++++++++++++++++++++++++++++++++++ str/bit62_test.go | 31 +++++++++++++++++++++++++++++++ str/entry.go | 4 ++++ str/entry_test.go | 3 +++ str/str.go | 3 --- str/str_test.go | 2 -- 6 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 str/bit62.go create mode 100644 str/bit62_test.go create mode 100644 str/entry.go create mode 100644 str/entry_test.go diff --git a/str/bit62.go b/str/bit62.go new file mode 100644 index 0000000..da9a1ac --- /dev/null +++ b/str/bit62.go @@ -0,0 +1,41 @@ +package str + +import ( + "math" + "strings" +) + +var Bit62Chars string = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + +// 翻转[]byte +func ByteReverse(a []byte) { + for left, right := 0, len(a)-1; left < right; left, right = left+1, right-1 { + a[left], a[right] = a[right], a[left] + } +} + +// int64 -> string +func (s *Str) Bit62Encode(num int64) string { + bytes := []byte{} + for num > 0 { + bytes = append(bytes, Bit62Chars[num%62]) + num = num / 62 + } + ByteReverse(bytes) + return string(bytes) +} + +// string -> int64 +func (s *Str) Bit62Decode(str string) int64 { + var num int64 + n := len(str) + for i := 0; i < n; i++ { + pos := strings.IndexByte(Bit62Chars, str[i]) + num += int64(math.Pow(62, float64(n-i-1)) * float64(pos)) + } + return num +} + +func (s *Str) Bit62Add(base, value string) string { + return s.Bit62Encode(s.Bit62Decode(base) + s.Bit62Decode(value)) +} diff --git a/str/bit62_test.go b/str/bit62_test.go new file mode 100644 index 0000000..a4699f4 --- /dev/null +++ b/str/bit62_test.go @@ -0,0 +1,31 @@ +package str + +import ( + "fmt" + "testing" +) + +func TestReverse(tt *testing.T) { + a := []byte("hello world") + fmt.Println(a) + //[104 101 108 108 111 32 119 111 114 108 100] + ByteReverse(a) + fmt.Println(a) + //[100 108 114 111 119 32 111 108 108 101 104] + +} + +func TestBit62Add(t *testing.T) { + var num int64 = 1234567890 + var num1 int64 = 5 + numB62 := StrTool.Bit62Encode(num) + num1B62 := StrTool.Bit62Encode(num1) + fmt.Println(numB62) + fmt.Println(num1B62) + num2B62 := StrTool.Bit62Add(numB62, num1B62) + num2 := StrTool.Bit62Decode(num2B62) + fmt.Println(num2B62) + fmt.Println(num2) + fmt.Println(StrTool.Bit62Add("ly7vp", "1")) + +} diff --git a/str/entry.go b/str/entry.go new file mode 100644 index 0000000..595166b --- /dev/null +++ b/str/entry.go @@ -0,0 +1,4 @@ +package str + +type Str struct { +} diff --git a/str/entry_test.go b/str/entry_test.go new file mode 100644 index 0000000..7349b1e --- /dev/null +++ b/str/entry_test.go @@ -0,0 +1,3 @@ +package str + +var StrTool Str diff --git a/str/str.go b/str/str.go index 95b1ec3..bc80611 100644 --- a/str/str.go +++ b/str/str.go @@ -2,9 +2,6 @@ package str import "encoding/hex" -type Str struct { -} - // struing to hex func (s *Str) HexEncode(str string) string { byteStr := []byte(str) diff --git a/str/str_test.go b/str/str_test.go index 7959170..e996a75 100644 --- a/str/str_test.go +++ b/str/str_test.go @@ -5,8 +5,6 @@ import ( "testing" ) -var StrTool Str - func TestHexEncode(t *testing.T) { fmt.Println(StrTool.HexEncode("hello world!")) //68656c6c6f20776f726c6421