This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathZeroWidth.go
88 lines (83 loc) · 1.97 KB
/
ZeroWidth.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"flag"
"fmt"
"os"
"strings"
)
func encode(text string) string {
var count int = 0
var bins, out string
out = " "
letter := []rune(text)
for i := 0; i < len(letter); i++ {
bins = ""
dec := int(letter[i]) //letter to number
for ii := 0; ii <= 22; ii++ {
if dec < 1<<uint(ii) {
break
}
count = ii
}
for ii := count; ii >= 0; ii-- { //decimal to binary
if dec-1<<uint(ii) >= 0 {
bins += "1"
dec -= 1 << uint(ii)
} else {
bins += "0"
}
}
out += bins + " "
}
en := strings.Replace(out, " ", "", -1) //replace
en = strings.Replace(en, "1", "", -1)
en = strings.Replace(en, "0", "", -1)
return en
}
func decode(text string) string {
var en, de string
var out int
rtext := []rune(text)
for i := 0; i < len(rtext); i++ { //remove other content
if string(rtext[i]) == "" {
en += ""
} else if string(rtext[i]) == "" {
en += ""
} else if string(rtext[i]) == "" {
en += ""
}
}
ren := strings.Replace(en, "", " ", -1) //replace
ren = strings.Replace(ren, "", "1", -1)
ren = strings.Replace(ren, "", "0", -1)
enb := strings.Fields(ren)
for i := 0; i < len(enb); i++ {
out = 0
bins := enb[i]
for ii := len(bins) - 1; ii >= 0; ii-- { //binary to decimal
if bins[:1] == "1" {
out += (1 << uint(ii))
}
bins = bins[1:]
}
de += string(out)
}
return de
}
func main() {
input_en := flag.String("e", "", "what you want to encode")
input_de := flag.String("d", "", "what you want to decode")
en_be := flag.String("b", "encoded content ->", "what you want to put before encoded content")
en_af := flag.String("a", "<- encoded content", "what you want to put before encoded content")
flag.Parse()
if *input_en == "" && *input_de == "" {
fmt.Println("Please use -h for help.")
os.Exit(128)
}
if *input_en != "" {
fmt.Printf("Encoded:\n%s%v%s", *en_be, encode(*input_en), *en_af)
}
if *input_de != "" {
fmt.Printf("Decoded:\n%v", decode(*input_de))
}
}