-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
141 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"unicode" | ||
) | ||
|
||
func StrToUnicode(str string) string { | ||
DD := []rune(str) //需要分割的字符串内容,将它转为字符,然后取长度。 | ||
finallStr := "" | ||
for i := 0; i < len(DD); i++ { | ||
if unicode.Is(unicode.Scripts["Han"], DD[i]) { | ||
textQuoted := strconv.QuoteToASCII(string(DD[i])) | ||
finallStr += textQuoted[1 : len(textQuoted)-1] | ||
} else { | ||
h := fmt.Sprintf("%x", DD[i]) | ||
finallStr += "\\u" + isFullFour(h) | ||
} | ||
} | ||
return finallStr | ||
} | ||
|
||
func isFullFour(str string) string { | ||
if len(str) == 1 { | ||
str = "000" + str | ||
} else if len(str) == 2 { | ||
str = "00" + str | ||
} else if len(str) == 3 { | ||
str = "0" + str | ||
} | ||
return str | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestStrToUnicode(t *testing.T) { | ||
uncodeStr := StrToUnicode("十大户¥@!#%……&……*()——+《》、,。、;‘、配【】") | ||
t.Logf(uncodeStr) | ||
uncodeStr = StrToUnicode("國語詞典") | ||
t.Logf(uncodeStr) | ||
} |