golang實現unicode碼和中文之間的轉換


 1 package main  2  3 import (  4 "fmt"  5 "strconv"  6 "strings"  7 )  8  9 func main() { 10 sText := "中文" 11 textQuoted := strconv.QuoteToASCII(sText) 12 textUnquoted := textQuoted[1 : len(textQuoted)-1] 13  fmt.Println(textUnquoted) 14 15 sUnicodev := strings.Split(textUnquoted, "\\u") 16 var context string 17 for _, v := range sUnicodev { 18 if len(v) < 1 { 19 continue 20  } 21 temp, err := strconv.ParseInt(v, 16, 32) 22 if err != nil { 23  panic(err) 24  } 25 context += fmt.Sprintf("%c", temp) 26  } 27  fmt.Println(context) 28 }

運行結果:

1 \u4e2d\u6587 2 中文


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM