/* 1 Converts single-byte characters in cExpression to double-byte characters. Supported for Locale ID only (specified with the nRegionalIdentifier or nRegionalIDType parameters). 2 Converts double-byte characters in cExpression to single-byte characters. Supported for Locale ID only (specified with the nRegionalIdentifier or nRegionalIDType parameters). 3 Converts double-byte Katakana characters in cExpression to double-byte Hiragana characters. Supported for Locale ID only (specified with the nRegionalIdentifier or nRegionalIDType parameters). 4 Converts double-byte Hiragana characters in cExpression to double-byte Katakana characters. Supported for Locale ID only (specified with the nRegionalIdentifier or nRegionalIDType parameters). 5 Converts double-byte characters to UNICODE (wide characters). 6 Converts UNICODE (wide characters) to double-byte characters. 7 Converts cExpression to locale-specific lowercase. Supported for Locale ID only (specified with the nRegionalIdentifier or nRegionalIDType parameters). 8 Converts cExpression to locale-specific uppercase. Supported for Locale ID only (specified with the nRegionalIdentifier or nRegionalIDType parameters). 13 Converts single-byte characters in cExpression to encoded base64 binary. 14 Converts base64 encoded data in cExpression to original unencoded data. 15 Converts single-byte characters in cExpression to encoded hexBinary. 16 Converts single-byte characters in cExpression to decoded hexBinary */ func Strconv(zstr string, zconvertType int) string { zcharset := "" switch zconvertType { case 9: //9 Converts double-byte characters in cExpression to UTF-8 zcharset = getCurrentCP() dec := mahonia.NewDecoder(zcharset) return dec.ConvertString(zstr) case 10: //10 Converts Unicode characters in cExpression to UTF-8 zcharset = "utf16" dec := mahonia.NewDecoder(zcharset) return dec.ConvertString(zstr) case 11: //11 Converts UTF-8 characters in cExpression to double-byte characters. zcharset = getCurrentCP() enc := mahonia.NewEncoder(zcharset) return enc.ConvertString(zstr) case 12: //12 Converts UTF-8 characters in cExpression to UNICODE characters. zcharset = "utf16" enc := mahonia.NewEncoder(zcharset) return enc.ConvertString(zstr) } return zstr }
func packEC(paramMap map[string]string) *Request { sid := make([]uint32, 1) sid[0] = 2014 urlPack := UrlPack{} //UrlPack{voice_search: 2} urlPack.Voice_search = 2 byteUrlPack, _ := mcpack.Marshal(urlPack) normalreq := Normalreq{} //{sid: sid, rsse_num: 10, rsse_type: 139} normalreq.Sid = sid normalreq.Rsse_num = 10 normalreq.Rsse_type = 139 normalreq.UrlPack = byteUrlPack if paramMap["character"] == "utf-8" || paramMap["character"] == "utf8" { enc := mahonia.NewEncoder("gbk") gbk := enc.ConvertString(paramMap["query"]) normalreq.Word = gbk } else { normalreq.Word = paramMap["query"] } req := Req{} req.Cmd = 0 req.Cmd64 = 0 cmdreq := make([]uint64, 2) cmdreq[0] = 0 cmdreq[1] = 2097152 req.Cmdreq = cmdreq req.Normalreq = normalreq request := Request{} reqList := make([]Req, 1) reqList[0] = req request.Req = reqList request.Req_module = "imms" request.Server = "mmsda" return &request }
func packCQ(paramMap map[string]string) *Request { sid := make([]uint32, 1) sid[0] = 2014 urlPack := UrlPackCQ{} //UrlPack{voice_search: 2} urlPack.Voice_search = 2 if _, ok := paramMap["speachid"]; ok { speachid, err := strconv.Atoi(paramMap["speachid"]) if err == nil { urlPack.Speachid = int32(speachid) } } enc := mahonia.NewEncoder("gbk") if _, ok := paramMap["lq"]; ok { if paramMap["character"] == "utf-8" || paramMap["character"] == "utf8" { mmsuse := enc.ConvertString(paramMap["lq"]) urlPack.Mmsuse = "lq@" + mmsuse } else { urlPack.Mmsuse = "lq@" + paramMap["lq"] } } else { if _, okbaidu := paramMap["baiduid"]; okbaidu { urlPack.Baiduid = paramMap["baiduid"] } } byteUrlPack, _ := mcpack.Marshal(urlPack) normalreq := Normalreq{} //{sid: sid, rsse_num: 10, rsse_type: 139} normalreq.Sid = sid normalreq.Rsse_num = 10 normalreq.Rsse_type = 139 normalreq.UrlPack = byteUrlPack if paramMap["character"] == "utf-8" || paramMap["character"] == "utf8" { //enc := mahonia.NewEncoder("gbk") gbk := enc.ConvertString(paramMap["query"]) normalreq.Word = gbk } else { normalreq.Word = paramMap["query"] } req := Req{} req.Cmd = 0 req.Cmd64 = 0 cmdreq := make([]uint64, 2) cmdreq[0] = 0 cmdreq[1] = 2097152 req.Cmdreq = cmdreq req.Normalreq = normalreq request := Request{} reqList := make([]Req, 1) reqList[0] = req request.Req = reqList request.Req_module = "imms" request.Server = "mmsda" return &request }
func main() { enc := mahonia.NewEncoder("gbk") gbk := enc.ConvertString("失落伐克欧洲杯") fmt.Println(gbk) //enc = mahonia.NewEncoder("utf8") dec := mahonia.NewDecoder("gbk") utf8 := dec.ConvertString(gbk) fmt.Println(utf8) }
func ExportCsv(columnNames []string, sql string, params []interface{}, db *sql.DB) ([]byte, error) { type TmpString struct { Value string } enc := mahonia.NewEncoder("gbk") rows, err := db.Query(sql, params...) if err != nil { return []byte{}, err } var buf = []byte{} for index, name := range columnNames { buf = append(buf, []byte(enc.ConvertString(name))...) if index != len(columnNames)-1 { buf = append(buf, []byte(";")...) } } buf = append(buf, []byte("\n")...) columns, err := rows.Columns() for rows.Next() { objects := []*TmpString{} tmpString := []interface{}{} for i := 0; i < len(columns); i++ { var tmp = new(TmpString) objects = append(objects, tmp) tmpString = append(tmpString, &tmp.Value) } err = PutRecord(rows, tmpString...) if err != nil { return nil, err } for index, object := range objects { buf = append(buf, []byte(enc.ConvertString(object.Value))...) if index != len(objects)-1 { buf = append(buf, []byte(";")...) } } buf = append(buf, []byte("\n")...) } return buf, nil }
/* getByteSlice converts value to byte slice according to given encoding and return a slice that is length equals to numberOfBytes or less if the string is shorter than numberOfBytes */ func (dt *DbfTable) convertToByteSlice(value string, numberOfBytes int) (s []byte) { e := mahonia.NewEncoder(dt.fileEncoding) b := []byte(e.ConvertString(value)) if len(b) <= numberOfBytes { s = b } else { s = b[0:numberOfBytes] } return }
func (dt *DbfTable) getNormalizedFieldName(name string) (s string) { e := mahonia.NewEncoder(dt.fileEncoding) b := []byte(e.ConvertString(name)) if len(b) > 10 { b = b[0:10] } d := mahonia.NewDecoder(dt.fileEncoding) s = d.ConvertString(string(b)) return }
// @Title Get Error Time Percent // @Description get rangetime to caculate errtime percent // @Param starttime query string "service starttime" // @Param endtime query string "service endtime" // @Param ns query string "ns name" // @Success 200 {object} models.FaultData // @Failure 403 body is empty // @router / func (c *DownController) GetErrPercent() { var starttime int64 var endtime int64 var ns string var err error var result models.Result ns = c.GetString("ns") nslist := strings.Split(ns, ".") if nslist[len(nslist)-1] != "com" { result.Success = false result.Data = "ns is not correct" c.Data["json"] = result c.Abort("400") } now := time.Now() days := time.Date(now.Year(), now.Month()+1, 0, 0, 0, 0, 0, time.UTC).Day() next_time := now.AddDate(0, 0, days) start_month := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local) end_month := time.Date(next_time.Year(), next_time.Month(), 1, 0, 0, 0, 0, time.Local) starttime, err = c.GetInt64("starttime") if err != nil { starttime = start_month.Unix() } endtime, err = c.GetInt64("endtime") if err != nil { endtime = end_month.Unix() } detail, err := models.GetErrTimeDetail(starttime, endtime, ns) c.Ctx.Output.Header("Content-Disposition", "attachment;filename=myfilename.csv") c.Ctx.Output.Header("Content-Type", "application/CSV") result2 := bytes.NewBufferString("") csvResult := csv.NewWriter(result2) csvResult.Write([]string{"影响业务", "发生时间", "事故内容", "影响面", "影响时长(min)", "事故原因", "对应方案", "未来对策", "定级"}) for _, oneDetail := range detail { csvResult.Write(oneDetail.GenCsv()) } csvResult.Flush() enc := mahonia.NewEncoder("gbk") output := enc.ConvertString(result2.String()) c.Ctx.Output.Body([]byte(output)) c.ServeJson() }
// 修改DNS func changeDNS(nic, dns string) error { var cmd = fmt.Sprintf(`netsh interface ipv4 set dnsservers name="%s" static %s primary`, nic, dns) enc := mahonia.NewEncoder("gbk") cmd = enc.ConvertString(cmd) utils.Logger.Debug(cmd) if output, err := utils.ExecCmd(scriptFile, cmd); err != nil { utils.Logger.Error(err.Error()) return nil } else { utils.Logger.Debug(string(output)) } return nil }
// 修改 IP func changeIP(nic, ip, netmask, gateway string) error { var cmd = fmt.Sprintf(`netsh interface ipv4 set address name="%s" source=static addr=%s mask=%s gateway=%s`, nic, ip, netmask, gateway) enc := mahonia.NewEncoder("gbk") cmd = enc.ConvertString(cmd) utils.Logger.Debug(cmd) if output, err := utils.ExecCmd(scriptFile, cmd); err != nil { utils.Logger.Error(err.Error()) return nil } else { utils.Logger.Debug(string(output)) } return nil }
func main() { sid := make([]uint32, 1) sid[0] = 2014 urlPack := UrlPack{} //UrlPack{voice_search: 2} urlPack.Voice_search = 2 byteUrlPack, _ := mcpack.Marshal(urlPack) normalreq := Normalreq{} //{sid: sid, rsse_num: 10, rsse_type: 139} normalreq.Sid = sid normalreq.Rsse_num = 10 normalreq.Rsse_type = 139 normalreq.UrlPack = byteUrlPack enc := mahonia.NewEncoder("gbk") gbk := enc.ConvertString("你好") normalreq.Word = gbk req := Req{} req.Cmd = 0 req.Cmd64 = 0 cmdreq := make([]uint64, 2) cmdreq[0] = 0 cmdreq[1] = 2097152 req.Cmdreq = cmdreq req.Normalreq = normalreq request := Request{} reqList := make([]Req, 1) reqList[0] = req request.Req = reqList request.Req_module = "imms" request.Server = "mmsda" mcRequet, _ := mcpack.Marshal(request) fmt.Println(len(mcRequet)) conn, err := mcpackrpc.Dial("tcp", "10.144.21.41:2987") if err != nil { fmt.Println(err) } fmt.Println("conn:", conn) connn, _ := conn.(io.ReadWriteCloser) clien := mcpackrpc.NewClientCodec(connn) fmt.Println("clien:", clien) }
func writeOutput(file *FilePath, pairTerms []*term.PairTerm, outputFreq int) { ext := filepath.Ext(file.filename) pos := strings.Index(file.filename, ext) base := string(file.filename[:pos]) outfile := base + ".csv" //fmt.Println("Outfile name:", outfile) outfile = filepath.Join(file.folder, outfile) //fmt.Println("Total words: ", len(pairTerms)) format := "%s,%d,%f\n" str := "短语,频率,分数\n" for _, pt := range pairTerms { if pt.GetFrequency() > outputFreq { str += fmt.Sprintf(format, pt.GetKey(), pt.GetFrequency(), pt.GetScore()) } } //fmt.Println(str) encoder := mahonia.NewEncoder(Encoding) str = encoder.ConvertString(str) util.WriteFile(outfile, str) log.Printf("短语存储到文件: %s", outfile) }
func main() { flag.Parse() var r io.Reader = os.Stdin var w io.Writer = os.Stdout if *from != "utf-8" { decode := mahonia.NewDecoder(*from) if decode == nil { log.Fatalf("Could not create decoder for %s", *from) } r = decode.NewReader(r) } if *to != "utf-8" { encode := mahonia.NewEncoder(*to) if encode == nil { log.Fatalf("Could not create decoder for %s", *to) } w = encode.NewWriter(w) } io.Copy(w, r) }
func main() { enc := mahonia.NewEncoder("gbk") fmt.Println(enc.ConvertString("hello,世界")) }
func NewFromFile(fileName string, fileEncoding string) (table *DbfTable, err error) { s, err := readFile(fileName) if err != nil { return nil, err } // Create and pupulate DbaseTable struct dt := new(DbfTable) dt.fileEncoding = fileEncoding dt.encoder = mahonia.NewEncoder(fileEncoding) dt.decoder = mahonia.NewDecoder(fileEncoding) // read dbase table header information dt.fileSignature = s[0] dt.updateYear = s[1] dt.updateMonth = s[2] dt.updateDay = s[3] dt.numberOfRecords = uint32(s[4]) | (uint32(s[5]) << 8) | (uint32(s[6]) << 16) | (uint32(s[7]) << 24) dt.numberOfBytesInHeader = uint16(s[8]) | (uint16(s[9]) << 8) dt.lengthOfEachRecord = uint16(s[10]) | (uint16(s[11]) << 8) // create fieldMap to taranslate field name to index dt.fieldMap = make(map[string]int) // Number of fields in dbase table dt.numberOfFields = int((dt.numberOfBytesInHeader - 1 - 32) / 32) // populate dbf fields for i := 0; i < int(dt.numberOfFields); i++ { offset := (i * 32) + 32 fieldName := strings.Trim(dt.encoder.ConvertString(string(s[offset:offset+10])), string([]byte{0})) dt.fieldMap[fieldName] = i var err error switch s[offset+11] { case 'C': err = dt.AddTextField(fieldName, s[offset+16]) case 'N': err = dt.AddNumberField(fieldName, s[offset+16], s[offset+17]) case 'F': err = dt.AddFloatField(fieldName, s[offset+16], s[offset+17]) case 'L': err = dt.AddBooleanField(fieldName) case 'D': err = dt.AddDateField(fieldName) } // Check return value for errors if err != nil { return nil, err } //fmt.Printf("Field name:%v\n", fieldName) //fmt.Printf("Field data type:%v\n", string(s[offset+11])) //fmt.Printf("Field length:%v\n", s[offset+16]) //fmt.Println("-----------------------------------------------") } //fmt.Printf("DbfReader:\n%#v\n", dt) //fmt.Printf("DbfReader:\n%#v\n", int(dt.Fields[2].fieldLength)) //fmt.Printf("num records in table:%v\n", (dt.numberOfRecords)) //fmt.Printf("lenght of each record:%v\n", (dt.lengthOfEachRecord)) // Since we are reading dbase file from the disk at least at this // phase changing schema of dbase file is not allowed. dt.dataEntryStarted = true // set DbfTable dataStore slice that will store the complete file in memory dt.dataStore = s return dt, nil }
畅酎抹亟赤装宁。 */ package elang import ( "bytes" "io" "github.com/axgle/mahonia" ) var ( // 正转 u | u2j | g2u = u | u2e u2g = mahonia.NewEncoder("gbk") j2u = mahonia.NewDecoder("euc-jp") // 逆转 u | u2g | j2u = u | e2u u2j = mahonia.NewEncoder("euc-jp") g2u = mahonia.NewDecoder("gbk") ) type Decoder struct { r io.Reader u2jw io.Writer g2ur io.Reader } func (this *Decoder) Read(p []byte) (n int, err error) { io.Copy(this.u2jw, this.r)
func main() { sid := make([]uint32, 1) sid[0] = 2014 urlPack := UrlPack{} //UrlPack{voice_search: 2} urlPack.Voice_search = 2 byteUrlPack, _ := mcpack.Marshal(urlPack) normalreq := Normalreq{} //{sid: sid, rsse_num: 10, rsse_type: 139} normalreq.Sid = sid normalreq.Rsse_num = 10 normalreq.Rsse_type = 139 normalreq.UrlPack = byteUrlPack // cd, err := iconv.Open("gbk", "utf-8") // if err != nil { // fmt.Println(err) // } // gbk := cd.ConvString("失落伐克欧洲杯") enc := mahonia.NewEncoder("gbk") gbk := enc.ConvertString("失落伐克欧洲杯") normalreq.Word = gbk //jsonnormalreq, _ := json.Marshal(normalreq) //fmt.Println(string(jsonnormalreq)) req := Req{} req.Cmd = 0 req.Cmd64 = 0 cmdreq := make([]uint64, 2) cmdreq[0] = 0 cmdreq[1] = 2097152 req.Cmdreq = cmdreq //req.Sub_cmdreq = cmdreq req.Normalreq = normalreq request := Request{} reqList := make([]Req, 1) reqList[0] = req request.Req = reqList request.Req_module = "imms" request.Server = "mmsda" /////////////////////// // jsonrequest, _ := json.Marshal(request) // fmt.Println(string(jsonrequest)) // content, _ := mcpack.Marshal(request) // fmt.Println(hex.Dump(content)) // msg := Request{} // mcpack.Unmarshal(content, &msg) // jsonMsg, _ := json.Marshal(msg) // fmt.Println(string(jsonMsg)) //return ///////////////////// server := []string{HostAdd} client := mcpacknpc.NewClient(server) reply := new(Reply) now := time.Now() nanos := now.UnixNano() num := make(chan int, 64) for i := 0; i < 300000; i++ { num <- i go mmsda(client, &request, reply, num) } end := now.UnixNano() fmt.Println("time", (end - nanos)) // client.Call(request, reply) // fmt.Println(reply) // jsonother, _ := json.Marshal(reply) // fmt.Println(string(jsonother)) // dec := mahonia.NewDecoder("gbk") // fmt.Println(dec.ConvertString(reply.Result[0].Normal.Danormalanalysis.Voiceguidesidentify.Fullreplace_query)) //fmt.Println(reply.Result[0].Normal.Danormalanalysis.Voiceguidesidentify.Fullreplace_query) }
//utf-8转gbk func Utf8ToGBK(str string) string { //字符集转换 enc := mahonia.NewEncoder("gbk") return enc.ConvertString(str) }
// Create a new dbase table from scratch func New(encoding string) (table *DbfTable) { // Create and populate DbaseTable struct dt := new(DbfTable) dt.fileEncoding = encoding dt.encoder = mahonia.NewEncoder(encoding) dt.decoder = mahonia.NewDecoder(encoding) // set whether or not this table has been created from scratch dt.createdFromScratch = true // read dbase table header information dt.fileSignature = 0x03 dt.updateYear = byte(time.Now().Year() % 100) dt.updateMonth = byte(time.Now().Month()) dt.updateDay = byte(time.Now().YearDay()) dt.numberOfRecords = 0 dt.numberOfBytesInHeader = 32 dt.lengthOfEachRecord = 0 // create fieldMap to taranslate field name to index dt.fieldMap = make(map[string]int) // Number of fields in dbase table dt.numberOfFields = int((dt.numberOfBytesInHeader - 1 - 32) / 32) s := make([]byte, dt.numberOfBytesInHeader) //fmt.Printf("number of fields:\n%#v\n", numberOfFields) //fmt.Printf("DbfReader:\n%#v\n", int(dt.Fields[2].fieldLength)) //fmt.Printf("num records in table:%v\n", (dt.numberOfRecords)) //fmt.Printf("lenght of each record:%v\n", (dt.lengthOfEachRecord)) // Since we are reading dbase file from the disk at least at this // phase changing schema of dbase file is not allowed. dt.dataEntryStarted = false // set DbfTable dataStore slice that will store the complete file in memory dt.dataStore = s dt.dataStore[0] = dt.fileSignature dt.dataStore[1] = dt.updateYear dt.dataStore[2] = dt.updateMonth dt.dataStore[3] = dt.updateDay // no MDX file (index upon demand) dt.dataStore[28] = 0x00 // set dbase language driver // Huston we have problem! // There is no easy way to deal with encoding issues. At least at the moment // I will try to find archaic encoding code defined by dbase standard (if there is any) // for given encoding. If none match I will go with default ANSI. // // Despite this flag in set in dbase file, I will continue to use provide encoding for // the everything except this file encoding flag. // // Why? To make sure at least if you know the real encoding you can process text accordingly. if code, ok := encodingTable[lookup[encoding]]; ok { dt.dataStore[28] = code } else { dt.dataStore[28] = 0x57 // ANSI } return dt }
func main() { sid := make([]uint32, 1) sid[0] = 2014 urlPack := UrlPack{} //UrlPack{voice_search: 2} urlPack.Voice_search = 2 byteUrlPack, err := mcpack.Marshal(urlPack) normalreq := Normalreq{} //{sid: sid, rsse_num: 10, rsse_type: 139} normalreq.Sid = sid normalreq.Rsse_num = 10 normalreq.Rsse_type = 139 normalreq.UrlPack = byteUrlPack // cd, err := iconv.Open("gbk", "utf-8") // if err != nil { // fmt.Println(err) // } // gbk := cd.ConvString("失落伐克欧洲杯") enc := mahonia.NewEncoder("gbk") gbk := enc.ConvertString("失落伐克欧洲杯") fmt.Println(gbk) normalreq.Word = "失落伐克欧洲杯" //gbk //jsonnormalreq, _ := json.Marshal(normalreq) //fmt.Println(string(jsonnormalreq)) req := Req{} req.Cmd = 0 req.Cmd64 = 0 cmdreq := make([]uint64, 2) cmdreq[0] = 0 cmdreq[1] = 2097152 req.Cmdreq = cmdreq //req.Sub_cmdreq = cmdreq req.Normalreq = normalreq request := Request{} reqList := make([]Req, 1) reqList[0] = req request.Req = reqList request.Req_module = "imms" request.Server = "mmsda" //==================== //jsonrequest, _ := json.Marshal(request) //fmt.Println(string(jsonrequest)) cont, err := mcpack.Marshal(request) if err != nil { fmt.Println("mcpack.Marshal====", err) } nh, err := nshead.Dial(HostAdd) if err != nil { fmt.Println("lianjie", err) } defer nh.Close() //nh.Provider = "noahdt" nh.MagicNum = nshead.DefaultMagicNum _, err = nh.Write(cont) if err != nil { fmt.Println("Write", err) return } fmt.Println(hex.Dump(cont)) msg := Request{} err = mcpack.Unmarshal(cont, &msg) if err != nil { fmt.Println("==============") } strMag, _ := json.Marshal(msg) fmt.Println("Unmarshal", string(strMag)) //return data, err := nh.ReadBody() if err != nil { fmt.Println("ReadBody", err) } reply := new(interface{}) err = mcpack.Unmarshal(data, reply) fmt.Printf("%p%v\n", reply, reply) }
package pinyin import ( "github.com/axgle/mahonia" ) //define func UTF8ToGBK by using mahonia package var enc = mahonia.NewEncoder("gbk") func UTF8ToGBK(s string) string { return enc.ConvertString(s) }