Csharp: Converting chinese character to Unicode, csharpconverting

Source: Internet
Author: User

Csharp: Converting chinese character to Unicode, csharpconverting

Function chinese2unicode(Str)    Dim Str_one:Str_one = ""    Dim Str_unicode:Str_unicode = ""    For i  = 1 To Len(Str)        Str_one = Mid(Str, i, 1)        If AscW(Str_one) < 0 or AscW(Str_one) > 255 Then            Str_unicode = Str_unicode & Chr(38)            Str_unicode = Str_unicode & Chr(35)            Str_unicode = Str_unicode & Chr(120)            Str_unicode = Str_unicode & Hex(AscW(Str_one))            Str_unicode = Str_unicode & Chr(59)        Else            Str_unicode = Str_unicode & Str_one        End If    Next    chinese2unicode=Str_unicodeEnd Function

 

/// <Summary> // % 26% 23x4EB2% 3B % 26% 23 x 7231% 3B % 26% 23 x 7684% 3B % 26% 23x4F1A % 3B % 26% 23 x 5458% 3 BTeresaLiu % 2C % 26% 23x516D % 3B % 26% 23x798F % 3B % 26% 23x73E0% 3B % 26% 23x5BF6% 3B % 26% 23 x 6703% 3B % 26% 23x54E1% 3B % 26% 23x5BC6% 3B % 26% 23x78BC % 3B % 26% 23x4FEE % 3B % 26% 23 x 6539% 3B % 26% 23 x 9805% 3B % 26% 23x901A % 3B % 26% 23x77E5% 3B % 26% 23xFF1A % 3B % 26% 23x95A3% 3B % 26% 23x4E0B % 3B % 26% 23x5DF2% 3B % 26% 23 x 6210% 3B % 26% 23x529F % 3B % 26% 23x66F4% 3B % 26% 23 x 6539% 3B % 26% 23x 5BC6% 3B % 26% 23x78BC % 3B % 26% 23xFF0C % 3B % 26% 23 x 5982% 3B % 26% 23 x 6709% 3B % 26% 23x67E5% 3B % 26% 23x8A62% 3B % 26% 23xFF0C % 3B % 26% 23x8ACB % 3B % 26% 23x81F4% 3B % 26% 23x96FB % 3B % 26% 23 x 9999% 3B % 26% 23x6E2F % 3b27213368% 26% 23xFF0F % 3B % 26% 23x4E2D % 3B % 26% 23x570B 3B4008846222 // juwen 20140724 /// </summary> /// <param name = "str"> </param> /// <returns> </returns> private string chinese2uncode (string str) {string s = ""; string outStr = ""; If (! String. isNullOrEmpty (str) {for (int I = 0; I <str. length; I ++) {if (Microsoft. visualBasic. strings. ascW (str [I]. toString () <0 | Microsoft. visualBasic. strings. ascW (str [I]. toString ()> 255) // if it is a Chinese conversion Regex. isMatch (str [I]. toString (), @ "[\ u4e00-\ u9fa5]") {// outStr + = "\ u" + (int) str [I]). toString ("x"); outStr = outStr + (char) 38; // "&"; // char (38); outStr = outStr + (char) 35; // "#"; outStr = outStr + (char) 120; // "x"; outStr = outStr + Microsoft. visualBasic. conversion. hex (Microsoft. visualBasic. strings. ascW (str [I]. toString (); // outStr + outStr = outStr + (char) 59; // "; // Str_unicode = Str_unicode & Chr (38) // Str_unicode = Str_unicode & Chr (35) // Str_unicode = Str_unicode & Chr (120) // Str_unicode = Str_unicode & Hex (AscW (Str_one )) // Str_unicode = Str_unicode & Chr (59) //;} else {outStr + = str [I] ;}}s = outStr; return s ;}

C # conversion of unicode to Chinese Characters

String newcontent = Encoding. Unicode. GetString (newa );

Add a breakpoint. What is the value of newa in this sentence?

C # How can unicode be converted to ansi without garbled characters?

The following example shows how to convert Unicode to Ansi:
Using System. IO;
Using System. Text;

// Convert the DBCS-932 encoded file to unicode-file
Encoding ecp932 = Encoding. GetEncoding (932 );
StreamReader sr = new StreamReader (@ "C: \ cp932.txt", ecp932, false );
StreamWriter sw = new StreamWriter (@ "C: \ unicode.txt", false,
Encoding. Unicode );
Sw. Write (sr. ReadToEnd ());
Sw. Close ();
Sr. Close ();

// Convert unicode-file to ANSI text file
Encoding ecp1252 = Encoding. GetEncoding (1252 );
Sr = new StreamReader (@ "C: \ unicode.txt", Encoding. Unicode, false );
Sw = new StreamWriter (@ "C: \ cp1252.txt", false, ecp1252 );
Sw. Write (sr. ReadToEnd ());
Sw. Close ();
Sr. Close ();

Refer to msdn
Support.microsoft.com/kb/138813

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.