diff --git a/.gitignore b/.gitignore index ca3d917..3b95b32 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ vendor hc* hex-convert .idea -dist \ No newline at end of file +dist +.vscode \ No newline at end of file diff --git a/assets/index.tpl b/assets/index.tpl index 7b60d60..02a3057 100644 --- a/assets/index.tpl +++ b/assets/index.tpl @@ -1,13 +1,11 @@ -

转换十进制

-
-
- +

转换进制

+ + + -
- -

转换二进制

-
-
- -
\ No newline at end of file diff --git a/converter/converter.go b/converter/converter.go index 353e530..a77271b 100644 --- a/converter/converter.go +++ b/converter/converter.go @@ -21,27 +21,29 @@ package converter import ( - "math" - "strconv" - "strings" - "github.com/ehlxr/hex-convert/metadata" + "strconv" ) func ToDecimal(scale int, data string) (int, error) { - var new_num float64 - new_num = 0.0 - nNum := len(strings.Split(data, "")) - 1 - for _, value := range strings.Split(data, "") { - tmp := float64(findkey(value)) - if tmp != -1 { - new_num = new_num + tmp*math.Pow(float64(scale), float64(nNum)) - nNum = nNum - 1 - } else { - break - } - } - return int(new_num), nil + // var newNum float64 + // newNum = 0.0 + // nNum := len(strings.Split(data, "")) - 1 + // for _, value := range strings.Split(data, "") { + // tmp := float64(findkey(value)) + // if tmp != -1 { + // newNum = newNum + tmp*math.Pow(float64(scale), float64(nNum)) + // nNum = nNum - 1 + // } else { + // break + // } + // } + // return int(newNum), nil + + intSize := 32 << uint(^uint(0)>>63) + num, err := strconv.ParseInt(data, scale, intSize) + + return int(num), err } func ToBinary(scale int, data string) (string, error) { @@ -82,19 +84,19 @@ func findkey(in string) int { } func fromDecimal(scale, data int) (string, error) { - new_num_str := "" + newNumStr := "" var remainder int - var remainder_string string + var remainderString string for data != 0 { remainder = data % scale if 76 > remainder && remainder > 9 { - remainder_string = metadata.TEN_TO_ANY[remainder] + remainderString = metadata.TEN_TO_ANY[remainder] } else { - remainder_string = strconv.Itoa(remainder) + remainderString = strconv.Itoa(remainder) } - new_num_str = remainder_string + new_num_str + newNumStr = remainderString + newNumStr data = data / scale } - return new_num_str, nil + return newNumStr, nil } diff --git a/gen/assets_vfsdata.go b/gen/assets_vfsdata.go index 22487b1..6aef50d 100644 --- a/gen/assets_vfsdata.go +++ b/gen/assets_vfsdata.go @@ -19,14 +19,14 @@ var Assets = func() http.FileSystem { fs := vfsgen۰FS{ "/": &vfsgen۰DirInfo{ name: "/", - modTime: time.Date(2018, 8, 2, 2, 1, 27, 751097893, time.UTC), + modTime: time.Date(2019, 9, 19, 8, 41, 27, 36642564, time.UTC), }, "/index.tpl": &vfsgen۰CompressedFileInfo{ name: "index.tpl", - modTime: time.Date(2018, 8, 2, 2, 1, 27, 751448710, time.UTC), - uncompressedSize: 563, + modTime: time.Date(2019, 9, 19, 8, 41, 27, 35059012, time.UTC), + uncompressedSize: 434, - compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xe4\xd0\xbd\x4e\xc3\x30\x10\x07\xf0\x3d\x4f\x71\xba\xa5\x20\xa1\x5a\xdd\x6d\x4f\x3c\x06\x8b\x93\x1c\x38\x22\xfe\x90\x7d\x46\x74\x83\x8d\x81\x8e\x9d\xd8\x98\x19\x3a\x31\x20\x10\x2f\x43\xa2\x3e\x06\x2a\x19\x22\x21\x10\x0f\xc0\x66\x59\xf7\xf1\xbf\x9f\xb4\x2b\xbd\x7f\x7b\x1a\x37\x8f\xc3\xe6\x76\xff\xfe\x30\xdc\x3d\x4b\x61\x57\xba\x92\xe7\x21\x39\x30\x0d\x77\xc1\x2b\x6c\x11\x1c\xb1\x0d\xad\xc2\x18\x32\xa3\xae\x00\x00\x64\xe7\x63\x61\xe0\x75\x24\x85\x4c\xd7\x8c\xe0\x8d\x23\x85\xb9\x31\x3d\x21\x04\x7f\x49\xeb\x12\x15\xb2\xed\xf2\xf2\xca\xf4\x85\xd4\xfc\x5c\x26\x8a\xbd\x69\xe8\x48\x9c\x9d\x8a\x8b\x93\xc5\xe2\x18\xe1\xeb\xc3\x86\xbe\xa5\xa4\x70\x8a\x33\x6e\x77\x28\xb4\xac\xd3\x1f\x3b\x5b\xc3\xe6\xdb\x80\x71\xbb\x1b\x6e\x5e\x51\xfc\xd0\x99\x4b\xed\x3a\x46\x98\x42\xe1\x44\x70\xa8\x94\xe2\x70\xb8\xae\xaa\x59\xe6\xe3\xe5\xfe\x77\x99\xfa\x7f\xc8\xc0\x4c\xf3\x19\x00\x00\xff\xff\xac\x32\x6c\x93\x33\x02\x00\x00"), + compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x94\xd1\xb1\x4a\xc5\x30\x14\x06\xe0\xd9\x3e\x45\x38\x2f\x10\xeb\x20\x0e\xc9\x79\x97\xd8\x1e\x69\x21\x6d\xc2\xcd\xa9\xe8\xa6\x60\xc1\xc1\x3b\x89\x8b\x6e\x82\x93\x8a\x4e\x0a\xa2\xd7\x97\x31\xd7\x3e\x86\xd4\x58\x70\x50\xf1\x66\x4c\xf8\xf2\x1f\xfe\xa3\xaa\x1c\x87\x97\x9b\xe5\xfc\x72\x78\xbd\x88\xc7\x0f\x4a\x56\x39\x66\x6a\xc7\xcd\x1a\x61\x0a\xae\x5d\xab\x21\x80\x68\x88\x2b\x57\x6a\xf0\x2e\x30\x60\xb6\xa6\x02\x59\x2a\x58\xb4\xa6\x21\x0d\xa1\x30\x96\x00\x33\xf1\xed\x28\xe7\x47\x2d\x76\x8d\xed\x48\xc3\x06\xe0\xdb\xd3\xc9\x94\x91\xde\xfe\x04\x5b\x80\xb1\xbf\xfe\x0f\x48\xa3\x50\x39\xc9\x7c\x1d\x30\xce\x0f\x57\xc8\xca\x37\x3f\x45\xec\x6f\x7f\x43\x4a\xa6\x94\x74\xa3\xea\xd6\x77\x2c\x78\xdf\x93\x06\xa6\x3d\x86\xaf\x22\x4a\xc3\x06\x84\xb7\xa6\xa0\xca\xd9\x92\x66\x1a\x86\xbb\xc7\x61\x71\x1a\xfb\xab\xb8\xe8\x53\xd1\xef\xe7\x47\xcb\xb3\xfb\x78\xf0\x0c\x42\xfe\xf0\x5f\xe8\xb6\x9b\x9a\x61\x9a\x2d\x19\x90\x98\x29\x39\x6e\x05\x3f\x02\x00\x00\xff\xff\xb3\x9f\x69\x73\xb2\x01\x00\x00"), }, } fs["/"].(*vfsgen۰DirInfo).entries = []os.FileInfo{ @@ -54,7 +54,7 @@ func (fs vfsgen۰FS) Open(path string) (http.File, error) { } return &vfsgen۰CompressedFile{ vfsgen۰CompressedFileInfo: f, - gr: gr, + gr: gr, }, nil case *vfsgen۰DirInfo: return &vfsgen۰Dir{ diff --git a/go.mod b/go.mod index c2a4217..6344393 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,8 @@ require ( github.com/mitchellh/mapstructure v0.0.0-20180715050151-f15292f7a699 // indirect github.com/pelletier/go-toml v1.2.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect + github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd // indirect github.com/spf13/afero v1.1.1 // indirect github.com/spf13/cast v1.2.0 // indirect github.com/spf13/cobra v0.0.3 @@ -22,3 +24,5 @@ require ( golang.org/x/text v0.3.0 // indirect gopkg.in/yaml.v2 v2.2.1 // indirect ) + +go 1.13 diff --git a/go.sum b/go.sum index ce92fa5..a3030d8 100644 --- a/go.sum +++ b/go.sum @@ -18,6 +18,10 @@ github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181 github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 h1:bUGsEnyNbVPw06Bs80sCeARAlK8lhwqGyi6UT8ymuGk= +github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg= +github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd h1:ug7PpSOB5RBPK1Kg6qskGBoP3Vnj/aNYFTznWvlkGo0= +github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw= github.com/spf13/afero v1.1.1 h1:Lt3ihYMlE+lreX1GS4Qw4ZsNpYQLxIXKBTEOXm3nt6I= github.com/spf13/afero v1.1.1/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cast v1.2.0 h1:HHl1DSRbEQN2i8tJmtS6ViPyHx35+p51amrdsiTCrkg= diff --git a/server/server.go b/server/server.go index 4c980dd..2ae61f6 100644 --- a/server/server.go +++ b/server/server.go @@ -45,7 +45,7 @@ func decimal(w http.ResponseWriter, req *http.Request) { } log.Println(result) - fmt.Fprintf(w, "首页
%s", strconv.Itoa(result)) + _, _ = fmt.Fprintf(w, "首页
%s", strconv.Itoa(result)) } func binary(w http.ResponseWriter, req *http.Request) { @@ -59,7 +59,29 @@ func binary(w http.ResponseWriter, req *http.Request) { } log.Println(result) - fmt.Fprintf(w, "首页
%s", result) + _, _ = fmt.Fprintf(w, "首页
%s", result) +} + +func submit(w http.ResponseWriter, req *http.Request) { + scale, _ := strconv.Atoi(req.FormValue("scale")) + data := req.FormValue("data") + + var bd, od, dd, hd string + if scale != 2 && scale != 8 && scale != 10 && scale != 16 { + bd, od, dd, hd = "", "", "", "" + } else { + bd, _ = converter.ToBinary(scale, data) + d, err := converter.ToDecimal(scale, data) + if err == nil { + dd = strconv.Itoa(d) + } + + od, _ = converter.ToOctal(scale, data) + hd, _ = converter.ToHex(scale, data) + } + + log.Printf("%s %s %s %s", bd, od, dd, hd) + _, _ = fmt.Fprintf(w, "首页
二进制:%s 八进制:%s 十进制:%s 十六进制:%s", bd, od, dd, hd) } func index(w http.ResponseWriter, r *http.Request) { @@ -74,13 +96,14 @@ func index(w http.ResponseWriter, r *http.Request) { panic(err) } - fmt.Fprintf(w, string(fd)) + _, _ = fmt.Fprintf(w, string(fd)) } func Start(host string, port int) error { http.HandleFunc("/", index) http.HandleFunc("/d", decimal) http.HandleFunc("/b", binary) + http.HandleFunc("/s", submit) addr := fmt.Sprintf("%s:%d", host, port) if strings.Contains(addr, "0.0.0.0") {