-
Notifications
You must be signed in to change notification settings - Fork 0
/
testEncodeDecode.go
61 lines (47 loc) · 1.45 KB
/
testEncodeDecode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"errors"
"fmt"
"math"
"math/rand"
"strings"
)
const (
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
length = uint64(len(alphabet))
)
func main() {
num := rand.Uint64()
// fmt.Println("New number to be encoded ", num)
// fmt.Println("Length of alphabet ", length)
// fmt.Println("Mod value ", 5577006791947779410%length)
// fmt.Println("Index value ", alphabet[40])
// fmt.Println("Divide value ", 5577006791947779410/length)
c := 'O' // rune (characters in Go are represented using `rune` data type)
asciiValue := int(c)
fmt.Printf("Ascii Value of %c = %d\n", c, asciiValue)
str := Encode(num)
val, _ := Decode(str)
fmt.Printf("Decoded number %c = %d\n", str, val)
}
func Encode(number uint64) string {
var encodedBuilder strings.Builder
encodedBuilder.Grow(11)
for ; number > 0; number = number / length {
fmt.Println("Inside loop ", number)
encodedBuilder.WriteByte(alphabet[(number % length)])
}
fmt.Println("After encoding ", encodedBuilder.String())
return encodedBuilder.String()
}
func Decode(encoded string) (uint64, error) {
var number uint64
for i, symbol := range encoded {
alphabeticPosition := strings.IndexRune(alphabet, symbol)
if alphabeticPosition == -1 {
return uint64(alphabeticPosition), errors.New("invalid character: " + string(symbol))
}
number += uint64(alphabeticPosition) * uint64(math.Pow(float64(length), float64(i)))
}
return number, nil
}