C # _ conversion between Chinese characters and GBK, Unicode, UTF-8 Encoding

Source: Internet
Author: User
IT development so far, many character encoding versions, currently popular GBK, Unicode, UTF-8 encoding and conversion of Chinese characters can be as follows Code : Private Void Button#click ( Object Sender, eventargs E)
{
// Convert Chinese characters to unicode encoding:
String Hz = textbox1.text. tostring ();
Byte [] B = encoding. Unicode. getbytes (HZ );
String O = "" ;
Foreach ( VaR X In B ){
O + = String . Format ( " {0: X2} " , X) + "   " ;
}
Textbox2.text = O;
}

Private Void Button2_click (Object Sender, eventargs E)
{
// Convert unicode encoding to Chinese characters:
String Cd = textbox2.text. tostring ();
String CD2 = CD. Replace ( "   " , "" );
CD2 = cd2.replace ( " \ R " , "" );
CD2 = cd2.replace ( " \ N " , "" );
CD2 = cd2.replace ( " \ R \ n " , "" );
CD2 = cd2.replace ( " \ T " , "" );
If (Cd2.length % 4 ! = 0 )
{
MessageBox. Show ( " Unicode encoding is double-byte. Please delete more or add less! Make sure it is a multiple of two. " );
}
Else
{
Int Len = cd2.length/ 2 ;
Byte [] B = New Byte [Len];
For (Int I = 0 ; I <cd2.length; I + = 2 )
{
String Bi = cd2.substring (I, 2 );
B [I/ 2 ] = (Byte ) Convert. toint32 (Bi, 16 );
}
String O = encoding. Unicode. getstring (B );
Textbox1.text = O;
}
}

Private Void Button5_click ( Object Sender, eventargs E)
{
// Convert Chinese characters to GBK hexadecimal codes:
String Hz = textbox3.text. tostring ();
Byte [] GBK = encoding. getencoding (" GBK " ). Getbytes (HZ );
String S1 = "" ; String S1d = "" ;
Foreach ( Byte B In GBK ){
// S1 + = convert. tostring (B, 16) + "";
S1 + = String . Format ( " {0: X2} " , B) + "   " ;
S1d + = B + "   " ;
Tooltip1.settooltip (textbox4, s1d );
}
Textbox4.text = S1;
Tooltip1.settooltip (textbox4, s1d );
// Convert Chinese characters to Unicode hexadecimal codes:
Byte [] UC = encoding. Unicode. getbytes (HZ );
String S2 = "" ; String S2d = "" ;
Foreach ( Byte B In UC)
{
// S2 + = convert. tostring (B, 16) + "";
S2 + = String . Format ( " {0: X2} " , B) + "   " ;
S2d + = B + "   " ;
Tooltip1.settooltip (textbox5, s2d );
}
Textbox5.text = S2;
Tooltip1.settooltip (textbox5, s2d );
// Chinese characters into UTF-8 hexadecimal code:
Byte [] Utf8 = encoding. utf8.getbytes (HZ );
String S3 = "" ; String S3d = "" ;
Foreach ( Byte B In Utf8)
{
// S3 + = convert. tostring (B, 16) + "";
S3 + = String . Format ( " {0: X2} " , B) + "   " ;
S3d + = B + "   " ;
Tooltip1.settooltip (textbox6, s3d );
}
Textbox6.text = S3;
Tooltip1.settooltip (textbox6, s3d );
}

Private Void Button6_click ( Object Sender, eventargs E)
{ // GBK hexadecimal code is converted into Chinese characters:
String Cd = textbox4.text. tostring ();
String [] B4 = CD. Split ( '   ' );
Byte [] BS = New Byte [ 2 ];
BS [ 0 ] = ( Byte ) Convert. tobyte (B4 [ 0 ], 16 );
BS [ 1 ] = (Byte ) Convert. tobyte (B4 [ 1 ], 16 );
Textbox3.text = encoding. getencoding ( " GBK " ). Getstring (BS );
}

Private Void Button7_click ( Object Sender, eventargs E)
{ // Unicode hexadecimal code to Chinese characters:
String Cd = textbox5.text. tostring ();
String [] B5 = CD. Split ( '   ' );
Byte [] BS = New Byte [ 2 ];
BS [ 0 ] = ( Byte ) Convert. tobyte (B5 [ 0 ], 16 );
BS [ 1 ] = ( Byte ) Convert. tobyte (B5 [ 1 ], 16 );
Textbox3.text = encoding. getencoding ( " Unicode " ). Getstring (BS );
}

Private Void Button8_click ( Object Sender, eventargs E)
{ // UTF-8 hexadecimal code converted into Chinese Characters:
String Cd = textbox6.text. tostring ();
String [] B6 = CD. Split ( '   ' );
Byte [] BS = New Byte [ 3 ];
BS [ 0 ] = ( Byte ) Convert. tobyte (B6 [ 0 ], 16 );
BS [ 1 ] = ( Byte ) Convert. tobyte (B6 [ 1 ], 16 );
BS [ 2 ] = ( Byte ) Convert. tobyte (B6 [ 2 ], 16 );
Textbox3.text = encoding. getencoding ( " UTF-8 " ). Getstring (BS );
}

URL: http://greatverve.cnblogs.com/archive/2012/03/02/csharp-gbk-unicode-utf-8.html

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.