Conversion between Perl Chinese/Unicode/utf8/gb2312

Source: Internet
Author: User

the conversion between Perl Chinese/Unicode/utf8/gb2312 encoding is a headache. I have summarized the following conversion methods for your reference.

# Author: jiangyujie <br/> Use utf8; <br/> Use encode; <br/> Use URI: escape; </P> <p> $/= "/N"; </P> <p> # utf8 encoding obtained from Unicode <br/> $ STR = '% u6536 '; <br/> $ STR = ~ S // % u ([0-9a-fa-f] {4})/Pack ("u", Hex ($1)/EG; <br/> $ STR = encode ("utf8", $ Str); <br/> Print UC unpack ("H *", $ Str ); </P> <p> # obtain gb2312 encoding From Unicode <br/> $ STR = '% u6536'; <br/> $ STR = ~ S // % u ([0-9a-fa-f] {4})/Pack ("u", Hex ($1)/EG; <br/> $ STR = encode ("gb2312", $ Str); <br/> Print UC unpack ("H *", $ Str ); </P> <p> # utf8 encoding obtained from Chinese <br/> $ STR = ""; <br/> Print uri_escape ($ Str ); </P> <p> # obtain Chinese characters from utf8 encoding <br/> $ utf8_str = uri_escape (""); <br/> Print uri_unescape ($ Str ); </P> <p> # obtain Perl Unicode from Chinese <br/> utf8: Decode ($ Str); <br/> @ chars = Split //, $ STR; <br/> foreach (@ chars) {<br/> printf "% x", ord ($ _ ); <Br/>}</P> <p> # obtain the standard Unicode from Chinese. <br/> $ A = "Chinese "; <br/> $ A = decode ("utf8", $ A); <br/> map {print "// U", sprintf ("% x ", $ _)} unpack ("u *", $ A); </P> <p> # obtain Chinese from standard Unicode <br/> $ STR = '% u6536 '; <br/> $ STR = ~ S // % u ([0-9a-fa-f] {4})/Pack ("u", Hex ($1)/EG; <br/> $ STR = encode ("utf8", $ Str); <br/> Print $ STR; </P> <p> # obtain Chinese from Perl Unicode <br/> my $ Unicode = "/X {505c}/X {8f66 }"; <br/> Print encode ("utf8", $ Unicode );

Related Article

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.