Golang convert between Unicode code and Chinese

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Converting Chinese to Unicode, using quotetoascii directly in the StrConv package in Golang, converting Unicode codes to Chinese is a bit trickier, first split the Unicode encoding by \u, and then use StrConv. Parseint, converts the 16 binary number to Int64, in the use of FMT. sprintf converts a number to a character and finally joins it together, so it becomes a Chinese string. The reference code is as follows:

1 Package Main2 3 Import (4     "FMT"5     "StrConv"6     "Strings"7 )8 9 Func Main () {TenStext: ="English" Onetextquoted: =StrConv. Quotetoascii (stext) Atextunquoted: = textquoted[1: Len (textquoted)-1] - FMT. Println (textunquoted) -  theSunicodev: = Strings. Split (textunquoted,"\\u") -     varContextstring -      for_, V: =Range Sunicodev { -         ifLen (v) <1 { +             Continue -         } +Temp, err: = StrConv. parseint (V, -, +) A         ifErr! =Nil { at Panic (Err) -         } -Context + = FMT. Sprintf ("%c", temp) -     } - FMT. Println (context) -}

Operation Result:

1 \u4e2d\u6587 2 English

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.