Csharp: string Encoding, csharpencoding

Source: Internet
Author: User

Csharp: string Encoding, csharpencoding

/// <Summary> /// convert Chinese to unicode /// </summary> /// <param name = "str"> </param> /// <returns> </returns> public static string unicode_0 (string str) {string outStr = ""; if (! String. isNullOrEmpty (str) {for (int I = 0; I <str. length; I ++) {outStr + = "/u" + (int) str [I]). toString ("x") ;}} return outStr ;} /// <summary> // convert Chinese characters to Unicode encoding /// </summary> /// <param name = "str"> </param> /// <returns> </returns> public static string bgktounicode (string str) {string outstr = ""; // convert Chinese characters to Unicode Encoding: string hz = str; byte [] B = Encoding. unicode. getBytes (hz); string o = ""; forea Ch (var x in B) {o + = string. format ("{0: X2}", x) + "" ;}outstr = o; return outstr ;} /// <summary> // convert unicode to Chinese // </summary> /// <param name = "str"> </param> /// <returns> </returns> public static string unicode_1 (string str) {string outStr = ""; if (! String. isNullOrEmpty (str) {string [] strlist = str. replace ("/",""). split ('U'); try {for (int I = 1; I <strlist. length; I ++) {// convert the unicode character to a 10-digit integer and convert it to the char Chinese Character outStr + = (char) int. parse (strlist [I], System. globalization. numberStyles. hexNumber) ;}} catch (FormatException ex) {outStr = ex. message ;}} return outStr ;}/// <summary> // convert unicode to Chinese (compliant with js Rules) /// </summary> /// <param name = "str"> </p Aram> // <returns> </returns> public static string unicode_js_1 (string str) {string outStr = ""; Regex reg = new Regex (@"(? I) \ u ([0-9a-f] {4}) "); outStr = reg. replace (str, delegate (Match m1) {return (char) Convert. toInt32 (m1.Groups [1]. value, 16 )). toString () ;}); return outStr ;}/// <summary> // convert Chinese to unicode (compliant with js Rules) /// </summary> /// <param name = "str"> </param> /// <returns> </returns> public static string unicode_js_0 (string str) {string outStr = ""; string a = ""; if (! String. isNullOrEmpty (str) {for (int I = 0; I <str. length; I ++) {if (Regex. isMatch (str [I]. toString (), @ "[\ u4e00-\ u9fa5]") {outStr + = "\ u" + (int) str [I]). toString ("x") ;}else {outStr + = str [I] ;}} return outStr ;} /// <summary> /// click the upper limit. // </summary> /// <param name = "utf8String"> </param> /// <returns> </returns> public static string unicodeTogbk (string utf8String) {string defaultString = ""; Encoding utf8 = Encoding. UTF8; Encoding defacode code = Encoding. default; // Convert the string into a byte []. byte [] utf8Bytes = Encoding. default. getBytes (utf8String); // Perform the conversion from one encoding to the other. byte [] defaultBytes = Encoding. convert (utf8, defaultCode, utf8Bytes); // Convert the new byte [] into a char [] and then into a string. // This is a slightly different approach to converting to pair strate // the use of GetCharCount/GetChars. char [] defaultChars = new char [defacode code. getCharCount (defaultBytes, 0, defaultBytes. length)]; defaultCode. getChars (defaultBytes, 0, defaultBytes. length, defaultChars, 0); defaultString = new string (defaultChars); return defastring string ;} /// <summary> /// click the upper limit. // </summary> /// <param name = "utf8String"> </param> /// <returns> </returns> public static string unicodeTogbkb (string utf8String) {string strBuffer = ""; byte [] buffer1 = Encoding. default. getBytes (utf8String); byte [] buffer2 = Encoding. convert (Encoding. UTF8, Encoding. default, buffer1, 0, buffer1.Length); strBuffer = Encoding. default. getString (buffer2, 0, buffer2.Length); return strBuffer ;}

 

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.